Skip to main content
Context Variables provide shared memory for your agents, allowing them to maintain state across a conversation and make decisions based on that shared information. If tools are the specialized capabilities agents can use, context variables are their collective knowledge base.

Introduction to Context Variables

Context Variables are a structured way to store and share information between agents in a group chat. They act as a persistent memory that:
  • Maintains state throughout the entire conversation
  • Is accessible to all agents in the group
  • Can be read and updated by any agent or tool
  • Stores data in a key-value format

The Hospital ER Analogy

Let’s return to our hospital emergency room analogy to understand context variables. Imagine a patient chart that follows them throughout their entire hospital visit. When a patient arrives at the ER:
  • The triage nurse records their symptoms, vital signs, and medical history
  • When the patient moves to a specialist, the chart goes with them
  • The specialist adds their observations and test results
  • If another specialist needs to see the patient, they have access to all previous notes
  • At discharge, the chart contains the complete history of the visit
In this analogy:
  • The patient chart is the context variables
  • Each medical professional is an agent
  • Adding notes to the chart is updating context variables
  • Reading previous entries is accessing context variables
  • The entire medical team shares one source of truth about the patient

The ContextVariables Class

Context variables in AG2 are implemented through the ContextVariables class, which provides a dictionary-like interface to store and retrieve values:

Core Functionality

Creating and Initializing Context Variables

You can create context variables when setting up your group chat pattern:

Reading and Writing Context Values

The ContextVariables class provides several methods for reading and writing values:

Dictionary-like Interface

The ContextVariables class implements a dictionary-like interface, allowing you to use a familiar syntax:

Persistence Across Agent Transitions

One of the most powerful features of context variables is their persistence across agent transitions. When control passes from one agent to another, the context variables go with it, maintaining the shared state. In the below example, the route_to_tech_support function updates the context variables with the current issue and passes the control to the tech support agent who can then access the updated context variables to make informed decisions.

Extending the Triage Example

Let’s extend our triage example to demonstrate how context variables enhance the customer support experience by maintaining information about the user and their issues. Here’s how we can enhance our triage system with context variables:
  • Create initial context variables with session information
  • Track and store each user query in the context
  • Record solutions provided by technical support
  • Intentionally print the context variables before and after processing in the functions for debugging

Example Output

When you run this code, you’ll see a workflow similar to this: !!! tip Search for the string context_variables.to_dict()= in the example output to check the values of the context variables.

Next Steps

Now that you understand Context Variables, the next section will explore Handoffs and Transitions - the mechanisms that control how agents pass control to each other in a group chat. In the Handoffs and Transitions section, you’ll learn:
  • How to define explicit handoff conditions between agents
  • How to use context variables to drive dynamic transitions
  • How to implement complex workflows with conditional branching
  • How to combine tools, context variables, and handoffs for sophisticated agent orchestration
The concepts you’ve learned about tools and context variables will be essential as we explore how to control the flow of conversation in complex multi-agent systems.