Agency Context
is a centralized data store accessible by all tools and agents within an agency. It allows you to share data between agents, control execution flow, and maintain state across tool calls without passing large data structures in messages.
Agency context is available when tools are deployed together with agents. If tools are deployed as separate APIs, they won’t share the same context, and you’ll need to implement your own state management solution.
Understanding Agency Context
Agency context is particularly useful when your agents interact with multiple tools that need to exchange information. Here’s why:- Without Agency Context: Suppose
Tool A
collects data thatTool B
needs. The agent must explicitly pass this data as a parameter toTool B
, consuming tokens and potentially hitting message limits.

- With Agency Context:
Tool A
can store the required data in the agency context, andTool B
can retrieve it without needing direct parameter passing. This approach reduces complexity, saves tokens, and enables additional workflows.

Using Agency Context
The agency context is accessible in both FunctionTools and BaseTools using.get
and .set
, which let you access and modify the MasterContext passed during execution.
Below is an example of how it can be used across tools.
In this example, calling the Query Database tool stores database context, which is later retrieved by the Answer Question tool.
- BaseTool
- Function Tool
Originally, in v0.x, shared data was stored in the special
You can also access the same context within the BaseTool by using
_shared_state
variable, which was carried over into v1.x. You can access it using the following methods:- Setting a value in the shared state:
_shared_state.set('key', value)
- Getting a value from the shared state:
_shared_state.get('key')
_shared_state
simply holds a pointer to the agency context, allowing you to share data between FunctionTools and BaseTools.You can also access the same context within the BaseTool by using
self.context
variable same way it’s done in FunctionTools.Advanced Agency Context Patterns
Complex Data Structures
Agency context can store any Python object, making it perfect for complex workflows:Workflow Coordination
Use agency context to coordinate multi-step workflows:Session Management
Agency context is perfect for maintaining session state:Best Practices
Use Descriptive Keys
Use clear, descriptive keys to avoid conflicts between different agents and workflows:Provide Default Values
Always provide sensible defaults when retrieving data:Clean Up Unneeded Data
For long-running sessions, clean up temporary data to avoid memory issues:Migrating tools
If you’re migrating from Agency Swarm v0.x, and want to use new FunctionTool instead of BaseTool, here’s how you can do that: BaseTool Pattern:Example: Complete Workflow
For a full example showing agency context in action, see the Agency Context Workflow Example which demonstrates:- Multi-step data collection and analysis
- Cross-agent data sharing
- Session management
- Workflow coordination
- Context monitoring and debugging