Skip to content

Commit d8b83ab

Browse files
authored
feat: Add documentation for ToolContext.invocation_state (#259)
Document how you can get and retrieve invocation_state when calling decorated tools Co-authored-by: Mackenzie Zastrow <[email protected]>
1 parent 0f71640 commit d8b83ab

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

docs/user-guide/concepts/tools/python-tools.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ asyncio.run(async_example())
148148

149149
### ToolContext
150150

151-
Tools can access their execution context by setting `context=True` and including a `tool_context` parameter. The `ToolContext` provides access to the invoking agent and current tool use data:
151+
Tools can access their execution context by setting `context=True` and including a `tool_context` parameter. The [`ToolContext`](../../../api-reference/types.md#strands.types.tools.ToolContext) provides access to the invoking agent, current tool use data, and invocation state:
152152

153153
```python
154154
from strands import tool, Agent, ToolContext
@@ -161,9 +161,15 @@ def get_self_name(tool_context: ToolContext) -> str:
161161
def get_tool_use_id(tool_context: ToolContext) -> str:
162162
return f"Tool use is {tool_context.tool_use["toolUseId"]}"
163163

164-
agent = Agent(tools=[get_self_name, get_tool_use_id], name="Best agent")
164+
@tool(context=True)
165+
def get_invocation_state(tool_context: ToolContext) -> str:
166+
return f"Invocation state: {tool_context.invocation_state["custom_data"]}"
167+
168+
agent = Agent(tools=[get_self_name, get_tool_use_id, get_invocation_state], name="Best agent")
169+
165170
agent("What is your name?")
166171
agent("What is the tool use id?")
172+
agent("What is the invocation state?", custom_data="You're the best agent ;)")
167173
```
168174

169175
### Tool Streaming

0 commit comments

Comments
 (0)