-
Notifications
You must be signed in to change notification settings - Fork 2.7k
docs: add ToolContext section for advanced tool metadata #1868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+45
−0
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ac49ac3
Update context.md
MuhammadSaqib786 71afa61
docs: update ToolContext example with WeatherContext per reviewer fee…
MuhammadSaqib786 e08e9e9
Merge branch 'main' into ToolContext
MuhammadSaqib786 acb1e13
Merge branch 'main' into ToolContext
MuhammadSaqib786 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,44 @@ if __name__ == "__main__": | |
4. The context is passed to the `run` function. | ||
5. The agent correctly calls the tool and gets the age. | ||
|
||
--- | ||
|
||
### Advanced: `ToolContext` | ||
|
||
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. | ||
For this, you can use the [`ToolContext`][agents.tool_context.ToolContext] class, which extends `RunContextWrapper`. | ||
|
||
```python | ||
from dataclasses import dataclass | ||
from agents import function_tool | ||
from agents.tool_context import ToolContext | ||
|
||
@dataclass | ||
class UserInfo: | ||
name: str | ||
uid: int | ||
|
||
@function_tool | ||
async def fetch_user_age(ctx: ToolContext[UserInfo]) -> str: | ||
"""Fetch the age of the user, with access to tool metadata.""" | ||
print(ctx.tool_name) # e.g. "fetch_user_age" | ||
print(ctx.tool_call_id) # unique ID for this invocation | ||
print(ctx.tool_arguments) # raw arguments as string | ||
return f"The user {ctx.context.name} is 47 years old." | ||
``` | ||
|
||
`ToolContext` provides the same `.context` property as `RunContextWrapper`, | ||
plus additional fields specific to the current tool call: | ||
|
||
- `tool_name` – the name of the tool being invoked | ||
This comment was marked as off-topic.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reviewing! Please let me know if there’s anything you’d like me to adjust or clarify. |
||
- `tool_call_id` – a unique identifier for this tool call | ||
- `tool_arguments` – the raw argument string passed to the tool | ||
|
||
Use `ToolContext` when you need tool-level metadata during execution. | ||
For general context sharing between agents and tools, `RunContextWrapper` remains sufficient. | ||
|
||
--- | ||
|
||
## Agent/LLM context | ||
|
||
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: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.