Composition: The Data Block via additional_context

build_context() returns the recalled-data block on its own, for when you want the data injected but not the tools - a read-only view of what the machine knows, placed exactly where you choose.

context_block.py
"""
Composition: The Data Block via additional_context
==================================================
build_context() returns the recalled-data block on its own, for when you
want the data injected but not the tools - a read-only view of what the
machine knows, placed exactly where you choose.

Run:
    .venvs/demo/bin/python cookbook/08_learning/11_composition/context_block.py
"""

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.learn import LearningMachine, LearningMode, UserMemoryConfig
from agno.models.openai import OpenAIResponses

db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")

learning = LearningMachine(
    db=db,
    model=OpenAIResponses(id="gpt-5.5"),  # the manual door injects nothing
    user_memory=UserMemoryConfig(mode=LearningMode.AGENTIC),
)

USER_ID = "composer@example.com"

# Seed a memory through the data API so the read-only agent has something to see
store = learning.user_memory_store
store.get_tools(user_id=USER_ID)[0]("Prefers conclusions first, then supporting detail")

# No learning=, no tools: just the data block, placed as additional context
agent = Agent(
    model=OpenAIResponses(id="gpt-5.5"),
    db=db,
    additional_context=learning.build_context(user_id=USER_ID),
    user_id=USER_ID,
    markdown=True,
)

if __name__ == "__main__":
    agent.print_response("Summarize why teams adopt vector databases.", stream=True)

The seed call uses the model-backed update_user_memory tool, so it needs a model key and can incur a model request before the agent runs. build_context() is evaluated once at agent construction here; rebuild it before later runs if the stored data changes.

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno "psycopg[binary]" openai sqlalchemy

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18

Run the example

Save the code above as context_block.py, then run:

python context_block.py

Full source: cookbook/08_learning/11_composition/context_block.py