-
Notifications
You must be signed in to change notification settings - Fork 94
Add multi-agent shared state docs #265
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,44 @@ Some events come in pairs, such as Before/After events. The After event callback | |
|
||
## Advanced Usage | ||
|
||
### Accessing Invocation State in Hooks | ||
|
||
Hook events that involve tool execution include access to `invocation_state`, which provides configuration and context data passed through the agent invocation. This is particularly useful for: | ||
|
||
1. **Configuration and Credentials**: Access API keys, database configurations, or other settings without hardcoding them | ||
2. **Request Context**: Access session IDs, user information, or request-specific data | ||
3. **Multi-Agent Shared State**: In [Graph](../multi-agent/graph.md) and [Swarm](../multi-agent/swarm.md) patterns, access state shared across all agents | ||
4. **Custom Parameters**: Pass any additional data that hooks might need | ||
|
||
```python | ||
from strands.experimental.hooks import BeforeToolInvocationEvent | ||
|
||
def log_with_context(event: BeforeToolInvocationEvent) -> None: | ||
"""Log tool invocations with context from invocation state.""" | ||
# Access invocation state from the event | ||
user_id = event.invocation_state.get("user_id", "unknown") | ||
session_id = event.invocation_state.get("session_id") | ||
environment = event.invocation_state.get("environment", "production") | ||
|
||
# Log with context | ||
logger.info( | ||
f"User {user_id} in session {session_id} ({environment}) " | ||
f"invoking tool: {event.tool_use['name']}" | ||
) | ||
|
||
# Register the hook | ||
agent = Agent(tools=[my_tool]) | ||
agent.hooks.add_callback(BeforeToolInvocationEvent, log_with_context) | ||
|
||
# Execute with context in invocation state | ||
result = agent( | ||
"Process the data", | ||
user_id="user123", | ||
session_id="sess456", | ||
environment="staging" | ||
Comment on lines
+165
to
+167
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. nit: Can we give an example where we are passing a non-json serializable object through invocation state? Right now, its not really clear what its benefit is over AgentState |
||
) | ||
``` | ||
|
||
### Fixed Tool Arguments | ||
|
||
Enforce specific arguments for tools, ensuring they always use particular values regardless of what the agent specifies: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,6 +319,38 @@ From [another_node_id]: | |
- [Agent name]: [Result text] | ||
``` | ||
|
||
## Shared State | ||
|
||
Graphs support passing shared state to all agents through the `invocation_state` parameter. This enables sharing configuration, credentials, and other data across agents without exposing it to the LLM. | ||
|
||
```python | ||
# Execute graph with shared state | ||
result = graph( | ||
"Analyze customer data", | ||
invocation_state={ | ||
"api_key": "secret-key-123", | ||
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. Can we remove references to api keys/tool-configs from invocation_state usages? The python-tools section update is good, but the examples throughout the other pages should be updated as well |
||
"database_config": {"host": "db.example.com", "port": 5432}, | ||
"session_id": "user-session-456" | ||
} | ||
) | ||
``` | ||
|
||
The `invocation_state` is automatically propagated to: | ||
|
||
- All agents in the graph via their `**kwargs` | ||
- Tools via `ToolContext` when using `@tool(context=True)` - see [Python Tools](../tools/python-tools.md#accessing-invocation-state-in-tools) | ||
- Tool-related hooks (BeforeToolInvocationEvent, AfterToolInvocationEvent) - see [Hooks](../agents/hooks.md#accessing-invocation-state-in-hooks) | ||
|
||
```python | ||
# Tools access shared state through ToolContext | ||
@tool(context=True) | ||
def api_call(endpoint: str, tool_context: ToolContext) -> str: | ||
api_key = tool_context.invocation_state.get("api_key") | ||
# Use api_key for authenticated requests... | ||
``` | ||
|
||
Use `invocation_state` for configuration and credentials that shouldn't appear in prompts, while using normal Graph input propagation for data the LLM should reason about. | ||
|
||
## Graphs as a Tool | ||
|
||
Agents can dynamically create and orchestrate graphs by using the `graph` tool available in the [Strands tools package](../tools/community-tools-package.md). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been updated, lets point to the main location