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_stateparameter onAgentprovides 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_stateparameter with a dictionary of default state variables. This will be the initial state. - You can pass
session_statetoagent.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_statewill 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_statein the system message, by referencing it in thedescriptionandinstructionsparameters. See the guide for more information. - You can have your Agent automatically update the session state by setting the
enable_agentic_stateparameter toTrue. See the guide for more information.
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.shopping_list.py
Agentic Session State
Agno provides a way to allow the Agent to automatically update the session state. Simply set theenable_agentic_state parameter to True.
agentic_session_state.py
Using state in instructions
You can reference variables from the session state in your instructions.state_in_instructions.py
Changing state on run
When you passsession_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 passsession_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
- View the Agent schema
- View the RunContext schema
- View Cookbook