Skip to content

Commit ac49ac3

Browse files
Update context.md
1 parent 095496e commit ac49ac3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/context.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,44 @@ if __name__ == "__main__":
6868
4. The context is passed to the `run` function.
6969
5. The agent correctly calls the tool and gets the age.
7070

71+
---
72+
73+
### Advanced: `ToolContext`
74+
75+
In some cases, you might want to access extra metadata about the tool being executed — such as its name, call ID, or raw argument string.
76+
For this, you can use the [`ToolContext`][agents.tool_context.ToolContext] class, which extends `RunContextWrapper`.
77+
78+
```python
79+
from dataclasses import dataclass
80+
from agents import function_tool
81+
from agents.tool_context import ToolContext
82+
83+
@dataclass
84+
class UserInfo:
85+
name: str
86+
uid: int
87+
88+
@function_tool
89+
async def fetch_user_age(ctx: ToolContext[UserInfo]) -> str:
90+
"""Fetch the age of the user, with access to tool metadata."""
91+
print(ctx.tool_name) # e.g. "fetch_user_age"
92+
print(ctx.tool_call_id) # unique ID for this invocation
93+
print(ctx.tool_arguments) # raw arguments as string
94+
return f"The user {ctx.context.name} is 47 years old."
95+
```
96+
97+
`ToolContext` provides the same `.context` property as `RunContextWrapper`,
98+
plus additional fields specific to the current tool call:
99+
100+
- `tool_name` – the name of the tool being invoked
101+
- `tool_call_id` – a unique identifier for this tool call
102+
- `tool_arguments` – the raw argument string passed to the tool
103+
104+
Use `ToolContext` when you need tool-level metadata during execution.
105+
For general context sharing between agents and tools, `RunContextWrapper` remains sufficient.
106+
107+
---
108+
71109
## Agent/LLM context
72110

73111
When an LLM is called, the **only** data it can see is from the conversation history. This means that if you want to make some new data available to the LLM, you must do it in a way that makes it available in that history. There are a few ways to do this:

0 commit comments

Comments
 (0)