Agents are able to maintain a conversation with one or more users. It is important to understand the following concepts:

  • Session: Each conversation with an Agent is called a session. Sessions are identified by a session_id and should be unique for each user.
  • Run: Every interaction (i.e. chat) within a session is called a run. Runs are identified by a run_id.
  • Messages: are the individual messages sent to and received from the model. They have a role (system, user or assistant) and content.

Below is an example where a single run is created with an Agent. A session_id is automatically generated and added to the run. This won’t be associated with any specific user.

from typing import Iterator
from agno.agent import Agent, RunResponse
from agno.models.openai import OpenAIChat
from agno.utils.pprint import pprint_run_response

agent = Agent(model=OpenAIChat(id="gpt-4o-mini"))

# Run agent and return the response as a variable
agent.print_response("Tell me a 5 second short story about a robot")

Multiple Users with Sessions

Sessions are unique for each user. If you want to maintain a conversation with multiple users, you can use the user_id and session_id to identify the session.

This requires storage of sessions. See storage for more information.

from typing import Iterator
from agno.agent import Agent, RunResponse
from agno.models.openai import OpenAIChat
from agno.utils.pprint import pprint_run_response
from agno.storage.postgres import PostgresStorage

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"), 
    storage=PostgresStorage(table_name="agent_sessions", db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
)

user_1_id = "user_101"
user_2_id = "user_102"

user_1_session_id = "session_101"
user_2_session_id = "session_102"

# Start the session with user 1
agent.print_response("Tell me a 5 second short story about a robot.", user_id=user_1_id, session_id=user_1_session_id)
# Continue the session with user 1
agent.print_response("Now tell me a joke.", user_id=user_1_id, session_id=user_1_session_id)

# Start the session with user 2
agent.print_response("Tell me about quantum physics.", user_id=user_2_id, session_id=user_2_session_id)
# Continue the session with user 2
agent.print_response("What is the speed of light?", user_id=user_2_id, session_id=user_2_session_id)

# Continue the session with user 1
agent.print_response("What can you tell me about what AIs are capable of?", user_id=user_1_id, session_id=user_1_session_id)