Skip to main content
Your Agents often need to access certain data during a run/session. It could be a todo list, the user’s profile, or anything else. When this data needs to remain accessible across runs, or needs to be updated during the session, you want to consider it session state. Session state is accessible from tool calls, pre-hooks and post-hooks, and other functions that are part of the Agent run. You are also able to use it in the system message, to ultimately present it to the Model. Session state is also persisted in the database, if one is available to the Agent, and is automatically loaded when the session is continued.
Understanding Agent “Statelessness”: Agents in Agno don’t maintain working state directly on the Agent object in memory. Instead they provide state management capabilities:
  • The session.get_session_state(session_id=session_id) method retrieves the session state of a particular session from the database
  • The session_state parameter on Agent provides the default state data for new sessions
  • Working state is managed per run and persisted to the database per session
  • The agent instance (or attributes thereof) itself is not modified during runs

State Management

Now that we understand what session state is, let’s see how it works:
  • You can set the Agent’s session_state parameter with a dictionary of default state variables. This will be the initial state.
  • You can pass session_state to agent.run(). This will take precedence over the Agent’s default state for that run.
  • You can access the session state in tool calls and other functions, via run_context.session_state.
  • The session_state will be stored in your database. Subsequent runs within the same session will load the state from the database. See the guide for more information.
  • You can use any data in your session_state in the system message, by referencing it in the description and instructions parameters. See the guide for more information.
  • You can have your Agent automatically update the session state by setting the enable_agentic_state parameter to True. See the guide for more information.
Here’s an example where an Agent is managing a shopping list:
session_state.py
The RunContext object is automatically passed to the tool as an argument. Any updates to run_context.session_state will automatically be persisted in the database and reflected in the session state. See the RunContext schema for more information.
Session state is also shared between members of a team when using Team. See Teams for more information.

Maintaining state across multiple runs

A big advantage of sessions is the ability to maintain state across multiple runs within the same session. For example, let’s say the agent is helping a user keep track of their shopping list.
You have to configure your storage via the db parameter for state to be persisted across runs. See Storage for more information.
shopping_list.py

Agentic Session State

Agno provides a way to allow the Agent to automatically update the session state. Simply set the enable_agentic_state parameter to True.
agentic_session_state.py
Don’t forget to set add_session_state_to_context=True to make the session state available to the agent’s context.

Using state in instructions

You can reference variables from the session state in your instructions.
Don’t use the f-string syntax in the instructions. Directly use the {key} syntax, Agno substitutes the values for you.
state_in_instructions.py

Changing state on run

When you pass session_id to the agent on agent.run(), the run will be part of the session with the given session_id. The state loaded from the database will be the state for that session. This is useful when you want to continue a session for a specific user.
changing_state_on_run.py

Overwriting the state in the db

By default, if you pass session_state to the run methods, this new state will be merged with the session_state in the db. You can change that behavior if you want to overwrite the session_state in the db:
overwriting_session_state_in_db.py

Developer Resources