Usage

Redis for Agent

Store agent sessions and run history in Redis with RedisDb.

RedisDb stores an Agent's sessions and run history in Redis.

Usage

Start in a virtual environment.

Install dependencies:

uv pip install agno redis openai ddgs

Export your OpenAI API key:

export OPENAI_API_KEY="your_openai_api_key_here"

Run Redis

Install Docker Desktop, then start Redis on port 6379:

docker run -d \
  --name my-redis \
  -p 6379:6379 \
  redis
redis_for_agent.py
from agno.agent import Agent
from agno.db.base import SessionType
from agno.db.redis import RedisDb
from agno.tools.websearch import WebSearchTools

db = RedisDb(db_url="redis://localhost:6379")

agent = Agent(
    db=db,
    tools=[WebSearchTools()],
    add_history_to_context=True,
)

agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")

all_sessions = db.get_sessions(session_type=SessionType.AGENT)
print(f"Stored sessions: {len(all_sessions)}")

if all_sessions:
    print(all_sessions[0])

Run the example

Save the code as redis_for_agent.py, start the database described above, and run:

python redis_for_agent.py

Parameters

ParameterTypeDefaultDescription
idOptional[str]-Database ID. Derived deterministically from the URL (or client representation) and key prefix when omitted.
redis_clientOptional[Union[Redis, RedisCluster]]-Redis client instance to use. If not provided a new client will be created from db_url. Required for Redis Cluster connections.
db_urlOptional[str]-Redis connection URL (e.g., "redis://localhost:6379/0" or "rediss://user:pass@host:port/db"). Supports single-node Redis only.
db_prefixstr"agno"Prefix for all Redis keys.
expireOptional[int]-TTL for Redis keys in seconds.
session_tableOptional[str]-Name of the table to store sessions.
runs_tableOptional[str]-Name of the table to store runs.
memory_tableOptional[str]-Name of the table to store memories.
metrics_tableOptional[str]-Name of the table to store metrics.
eval_tableOptional[str]-Name of the table to store evaluation runs.
knowledge_tableOptional[str]-Name of the table to store knowledge documents.
traces_tableOptional[str]-Name of the table to store traces.
spans_tableOptional[str]-Name of the table to store spans.