Usage
Firestore for Agent
Store agent sessions in Firestore with FirestoreDb.
Agno supports using Firestore as a storage backend for Agents using the FirestoreDb class.
Usage
Start in a virtual environment and set the model key before running the example.
Set OpenAI Key
Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.
export OPENAI_API_KEY=sk-***You need to provide either project_id or db_client to the FirestoreDb class. Configure Application Default Credentials (gcloud auth application-default login locally, or GOOGLE_APPLICATION_CREDENTIALS for a service account). The project must already have a Firestore database and your identity must have data access. The client does not provision the project or database.
from agno.agent import Agent
from agno.db.firestore import FirestoreDb
from agno.tools.websearch import WebSearchTools
PROJECT_ID = "agno-os-test" # Use your project ID here
# Setup the Firestore database
db = FirestoreDb(project_id=PROJECT_ID)
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?")Prerequisites
- Ensure your gcloud project is enabled with Firestore. See the Firestore documentation
- Install dependencies:
uv pip install agno openai google-cloud-firestore ddgs agno - Make sure your gcloud project is set up and you have the necessary permissions to access Firestore
Params
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | Database ID. Derived deterministically from project or client when omitted. |
db_client | Optional[Client] | - | The Firestore client to use. |
project_id | Optional[str] | - | The GCP project ID for Firestore. |
session_collection | Optional[str] | - | Name of the collection to store sessions. |
runs_collection | Optional[str] | None | Storage name for individual runs. Defaults to agno_runs, or <session_collection>_runs when a custom session name is supplied. |
memory_collection | Optional[str] | - | Name of the collection to store memories. |
metrics_collection | Optional[str] | - | Name of the collection to store metrics. |
eval_collection | Optional[str] | - | Name of the collection to store evaluation runs. |
knowledge_collection | Optional[str] | - | Name of the collection to store knowledge documents. |
traces_collection | Optional[str] | - | Name of the collection to store traces. |
spans_collection | Optional[str] | - | Name of the collection to store spans. |
Run the Example
Save the code as firestore_for_agent.py, complete the prerequisites above, then run:
python firestore_for_agent.py