This project contains a sample agent built with ADK Python that demonstrates how to use Redis for session state management.
This sample agent shows how you can integrate a custom session service with the ADK to persist conversation state in a Redis database. This allows for scalable and persistent user sessions.
The project is organized as follows:
./redis-session-service
├── adk_cli.py
├── README.md
├── notebooks
│ └── get_started_with_adk_redis_session_service.ipynb
└── redis_session_service
├── __init__.py
├── agent.py
├── .env.example
├── log_tools.py
└── requirements.txt
adk_cli.py: A custom command-line interface script that registers the Redis session service with the ADK.redis_session_service/: The main application directory containing the agent definition and supporting files.redis_session_service/agent.py: Defines the simple QA agent.redis_session_service/requirements.txt: Lists the Python dependencies for the project.notebooks/: Contains a Jupyter notebook for an interactive walkthrough of the service.
To run this agent, you will need:
-
Clone this repository and navigate to this subdirectory.
-
Set up environment variables:
Copy the
.env.examplefile to.envand fill in your Google Cloud project details.cp redis_session_service/.env.example redis_session_service/.env
Your
redis_session_service/.envfile should look like this:GOOGLE_GENAI_USE_VERTEXAI=1 GOOGLE_CLOUD_PROJECT="your-gcp-project-id" GOOGLE_CLOUD_LOCATION="your-gcp-location (e.g., us-central1)" -
Install dependencies:
Create a virtual environment and install the required packages.
uv venv source .venv/bin/activate uv pip install -r redis_session_service/requirements.txt -
Run the ADK web UI:
Start the ADK web server, pointing to the agent directory and specifying the Redis session service URI. The
adk_cli.pyscript ensures that theredis://protocol is recognized.uv run python adk_cli.py web --session_service_uri="redis://localhost:6379" redis_session_serviceNavigate to
127.0.0.1:8000in your browser to interact with the agent.
As you interact with the agent, its session state will be stored in your Redis database. You can inspect this data using the redis-cli.
-
Connect to Redis:
redis-cli
-
List all keys to find the session IDs:
KEYS "session:*" -
Get the data for a specific session: Replace
<session_id>with an ID from the previous step.GET "session:<session_id>"
This will return a JSON object containing the session state, including the conversation history.
You can deploy the agent to Cloud Run for a scalable setup. This requires a Redis instance accessible from your Cloud Run service.
Before deploying, you need a Redis instance. For production, it's recommended to use a managed service like Google Cloud Memorystore for Redis.
- Create a Memorystore for Redis instance within the same VPC network you plan to use for Cloud Run.
- Once created, note its IP address and port. Your Redis URI will be
redis://<redis_ip_address>:<redis_port>.
- Authenticate with Google Cloud:
gcloud auth login
- Set Environment Variables:
export GOOGLE_CLOUD_PROJECT="your-gcp-project-id" export GOOGLE_CLOUD_LOCATION="us-central1" # The Redis instance must be accessible from this VPC export VPC_NETWORK="your-vpc-network" export VPC_SUBNET="your-private-subnet-for-redis" # The Redis URI from the previous step export REDIS_URI="redis://<your-redis-ip>:<your-redis-port>"
- Run the Deployment Script:
From the project root directory (
./my-adk-python-samples), run the following command. The script handles containerization and deployment. The--run-commandargument is used to start the ADK server with the correct Redis session URI.python deploy_to_cloud_run.py --agent-folder=agent-memory/redis-session-service/redis_session_service \ --project=$GOOGLE_CLOUD_PROJECT \ --region=$GOOGLE_CLOUD_LOCATION \ --service-name="redis-session-agent" \ --vpc-egress="all-traffic" \ --network=$VPC_NETWORK \ --subnet=$VPC_SUBNET \ --with-ui \ --allow-unauthenticated \ --log-level DEBUG \ --adk-cli-path=agent-memory/redis-session-service/adk_cli.py \ --session-service-uri="$REDIS_URI" - The
--vpc-egressflag configures the necessary Serverless VPC Access Connector so the agent can communicate with the Redis instance on your VPC. - Once complete, the script will provide a public URL to access your agent.
- (adk-python) Support to configure Redis as the session storage
- ADK Python Community Contributions
- 📓 Get started with Memory Bank on ADK
- 🎥 How to build AI agents with memory
- Remember this: Agent state and memory with ADK (2025-08-02)
- 📦 Redis Agent Memory Server: A memory layer for AI agents using Redis as the vector database.