> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bindai.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 03.3 tool context

# Tool Context

Tool Context provides tools with access to the current execution environment.

Instead of relying only on function parameters, a tool can read and update workflow variables, access runtime metadata, inspect execution state, and interact with the surrounding agent or workflow.

This makes tools context-aware and enables more advanced automation scenarios.

***

# What is Tool Context?

A tool context contains information about the current execution.

Typical information includes:

* workflow variables
* execution metadata
* runtime state
* user input
* shared values
* workflow instance information

Rather than passing everything as function arguments, BindAI provides this information through the execution context.

***

# Why Use Tool Context?

Tool Context allows tools to:

* access values created by previous nodes
* share information between tools
* read workflow state
* update execution variables
* participate in complex workflows

Without a context object, every required value would have to be passed explicitly.

***

# Execution Context

Internally, BindAI executes tools using an execution context.

```text id="l7v2qp" theme={null}
Workflow

↓

Execution Context

↓

Tool
```

The context travels through the execution pipeline and is shared by all nodes involved in the current execution.

***

# Reading Variables

Tools can read values stored in the context.

Example:

```python id="o3kydt" theme={null}
name = context.variables.get(
    "customer_name",
)
```

If a previous workflow step stored this variable, the tool can immediately use it.

***

# Writing Variables

Tools can also store new values.

```python id="d5x8mv" theme={null}
context.variables.set(
    "customer_id",
    42,
)
```

Subsequent nodes or tools can retrieve the same value.

***

# Sharing Data Between Tools

Example flow:

```text id="hs4a2j" theme={null}
Search Customer

↓

customer_id

↓

Load Orders

↓

order_summary

↓

Generate Response
```

Each tool contributes information to the shared execution context.

***

# Workflow Integration

When tools run inside workflows, they automatically participate in the workflow state.

For example:

```text id="cm5hpn" theme={null}
Start

↓

Tool A

↓

Tool B

↓

Agent

↓

End
```

Variables created by Tool A are immediately available to Tool B and the agent.

***

# Agent Integration

Agents also use the execution context.

Typical values include:

* current user input
* previous tool outputs
* structured results
* execution metadata

This allows tools to cooperate naturally with the agent's reasoning process.

***

# Example

Suppose a previous workflow node stored a customer identifier.

```python id="w2mk9x" theme={null}
context.variables.set(
    "customer_id",
    1001,
)
```

A tool can retrieve it later.

```python id="j6e4fw" theme={null}
customer_id = context.variables.get(
    "customer_id",
)
```

No additional parameters are required.

***

# Metadata

The execution context may also contain metadata.

Typical examples include:

* timestamps
* execution identifiers
* workflow IDs
* custom runtime information

Applications can store metadata alongside variables when needed.

***

# Avoid Global State

Tool Context replaces global variables.

Instead of:

```python id="z1g8hv" theme={null}
GLOBAL_CUSTOMER = ...
```

Prefer:

```python id="te9kr4" theme={null}
context.variables.set(
    "customer",
    value,
)
```

This keeps executions isolated and thread-safe.

***

# Context Lifetime

The context exists only for the current execution.

```text id="px4a6u" theme={null}
Execution Starts

↓

Context Created

↓

Workflow Executes

↓

Execution Ends

↓

Context Disposed
```

Data is not shared automatically across different workflow executions.

***

# Memory vs Context

Tool Context and Memory serve different purposes.

| Tool Context                 | Memory                        |
| ---------------------------- | ----------------------------- |
| Exists only during execution | Persists across conversations |
| Stores workflow variables    | Stores conversational history |
| Temporary                    | Long-term                     |
| Shared between nodes         | Shared between sessions       |

Use Tool Context for execution state and Memory for persistent knowledge about previous interactions.

***

# Best Practices

* Use context for temporary execution data.
* Keep variable names descriptive.
* Store only information needed by later steps.
* Avoid large binary objects.
* Prefer context over global variables.
* Do not use context as permanent storage.

***

# Summary

Tool Context provides a shared execution environment for tools, agents, and workflows.

It enables tools to exchange information, access workflow state, and participate in complex execution pipelines without requiring every value to be passed explicitly.
