Skip to main content
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 that Tool B needs. The agent must explicitly pass this data as a parameter to Tool B, consuming tokens and potentially hitting message limits.
Without Agency Context
  • With Agency Context: Tool A can store the required data in the agency context, and Tool B can retrieve it without needing direct parameter passing. This approach reduces complexity, saves tokens, and enables additional workflows.
With Agency Context

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.
For BaseTools, access the agency context using self.context.

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:
FunctionTool 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
Agency context eliminates the need for complex parameter passing and enables multi-agent workflows while maintaining clean separation of concerns.