Code

cookbook/agent_concepts/storage/mongodb_storage.py
from agno.agent import Agent
from agno.storage.agent.mongodb import MongoDbAgentStorage
from agno.tools.duckduckgo import DuckDuckGoTools

# MongoDB connection settings
db_url = "mongodb://localhost:27017"

agent = Agent(
    storage=MongoDbAgentStorage(
        collection_name="agent_sessions", db_url=db_url, db_name="agno"
    ),
    tools=[DuckDuckGoTools()],
    add_history_to_messages=True,
)
agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.

2

Set your API key

export OPENAI_API_KEY=xxx
3

Install libraries

pip install -U openai pymongo agno
4

Run Agent