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.

firestore_for_agent.py
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

  1. Ensure your gcloud project is enabled with Firestore. See the Firestore documentation
  2. Install dependencies: uv pip install agno openai google-cloud-firestore ddgs agno
  3. Make sure your gcloud project is set up and you have the necessary permissions to access Firestore

Params

ParameterTypeDefaultDescription
idOptional[str]-Database ID. Derived deterministically from project or client when omitted.
db_clientOptional[Client]-The Firestore client to use.
project_idOptional[str]-The GCP project ID for Firestore.
session_collectionOptional[str]-Name of the collection to store sessions.
runs_collectionOptional[str]NoneStorage name for individual runs. Defaults to agno_runs, or <session_collection>_runs when a custom session name is supplied.
memory_collectionOptional[str]-Name of the collection to store memories.
metrics_collectionOptional[str]-Name of the collection to store metrics.
eval_collectionOptional[str]-Name of the collection to store evaluation runs.
knowledge_collectionOptional[str]-Name of the collection to store knowledge documents.
traces_collectionOptional[str]-Name of the collection to store traces.
spans_collectionOptional[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