Variables

Variables are used to store and retrieve runtime conversation data. This enables agents to remember information across conversational turns, leading to more contextual interactions.

Variable data

Variables have the following data:

  • Name: Variable name using snake case
  • Type: Variable type:
    • Text: String values
    • Number: Numeric values
    • Yes/No: Boolean values
    • Custom Object: You provide a schema for the object
    • List: List of variables. Provide values as a comma delimited list.
  • Default value: Default value for the variable
  • Description: Optional description of the variable

Variable references in instructions

To reference a variable by name in your instructions, use braces: {variable_name}.

How variables are used in prompts

When you reference a variable in agent instructions using the {variable_name} syntax, this does not perform a direct text replacement within the instruction prompt sent to the model. Here's the actual behavior:

  • When a variable is referenced within an agent's instructions, the system tracks this value while the agent is active.
  • If the value of a tracked variable is updated (typically by a tool or callback), the new value is appended to the conversation history that is provided to the model for that particular agent.
  • These variable value changes in conversation history give the model the necessary context about the state of these variables. The model can then use this information to generate accurate responses.

Updating variable values

The agent itself cannot update the value of a variable, but tools and callbacks can update variable values.

Agent variables use the ADK context state, which can be used to update variables. A global variable called context is available to use in your Python tool code. For example, you can use the following statement in a Python tool to update a variable using ADK features:

context.state["variable_name"] = value