Chapter 12·Part IIIAgentic architecture

Memory, Planning, and Reasoning

Persistent and semantic memory: an agent that remembers the FY2024 Q3 experience adjustment across processes via SQLite, and a vector store over the synthetic experience study archive.

In the book this is Part III, Agentic AI: From Concept to Architecture.

What you’ll build

  • Short-term, long-term, and episodic memory as distinct mechanisms
  • Context window management and summarisation at task boundaries
  • Vector stores, structured databases, and knowledge graphs for long-term memory
  • Planning versus reasoning, and hierarchical task decomposition
  • Reasoning traces as auditable and correctable evidence
  • Self-reflection before committing to an output

01_sqlite_memory.py

live agent on GeminiSource on GitHub

Two turns against SQLite-backed memory; the second recalls the first.

about 45s · live model callOpen in Colab
# Long-term memory: a file-backed store that survives across runs
# Book reference: Chapter 12, §12.3 "Long-Term Memory"
#
# ⚠ API COMPATIBILITY NOTE (see ERRATA in the root README):
# The printed listing uses `from agno.memory import Memory` and
# `Agent(memory=Memory(db=...))`. That class was removed in later Agno
# 2.x releases. The current equivalent — same behaviour, same SQLite
# persistence keyed to user_id — is `Agent(db=SqliteDb(...),
# enable_user_memories=True)`, used below. This is exactly the
# version-pinning re-test the book's TECHNICAL NOTE anticipates.
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))  # for common/
from common.config import get_model

# The .db file is the persistence layer; it lives on disk between runs
experience_study_db = SqliteDb(db_file="meridian_xs_memory.db")

# The agent now reads and writes memory keyed to the actuary's user_id
life_valuation_agent = Agent(
    model=get_model(),
    db=experience_study_db,          # was: memory=Memory(db=...) in print
    enable_user_memories=True,       # persistent memory across runs
    user_id="mumbai_life_valuation",
    tool_call_limit=10,
    markdown=True,
)

if __name__ == "__main__":
    # First run: give the agent something worth remembering.
    life_valuation_agent.print_response(
        "Remember: the FY2024 Q3 experience adjustment factor for term life "
        "India is 0.95, per study TL_EXP_2023.",
        stream=True,
    )
    # Second run — a NEW process would recall this from the .db file.
    life_valuation_agent.print_response(
        "What adjustment factor applies to term life India this cycle, and "
        "which study governs it?",
        stream=True,
    )

Runs the unmodified chapter script on the server and streams the agent's tool calls and reasoning here.

02_vector_knowledge.py

run in ColabSource on GitHub

Embeds the experience-study archive into Chroma and answers from it.

# Long-term memory (semantic): a vector store of prior study reports
# Book reference: Chapter 12, §12.3 "Long-Term Memory"
# Repo notes:
#   - The ./xs_reports/fy2024/ archive ships in the repo's data directory;
#     the path below points there.
#   - ChromaDb requires the `chromadb` package (a project dependency).
#   - ChromaDb defaults to an OpenAI embedder; the GeminiEmbedder is
#     passed explicitly so the whole example runs on the one
#     GOOGLE_API_KEY the repo already needs. NB: the embedder stays
#     Google-pinned even when MODEL_PROVIDER switches the chat model —
#     embeddings must match the index, so this script always needs
#     GOOGLE_API_KEY regardless of provider.
#   - add_content is synchronous here; indexing runs once at build.
import os

from agno.agent import Agent
from agno.knowledge.embedder.google import GeminiEmbedder
from agno.knowledge.knowledge import Knowledge
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))  # for common/
from common.config import get_model
from agno.vectordb.chroma import ChromaDb

DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data")

prior_study_archive = Knowledge(
    name="meridian_term_life_xs_archive",
    vector_db=ChromaDb(
        collection="term_life_india_xs",
        path="./xs_index",
        embedder=GeminiEmbedder(),      # embeddings via GOOGLE_API_KEY
    ),
)

# Index the FY2024 cycle reports once at build; query at runtime
prior_study_archive.add_content(path=os.path.join(DATA_DIR, "xs_reports", "fy2024"))

life_valuation_agent = Agent(
    model=get_model(),
    knowledge=prior_study_archive,
    tool_call_limit=10,
    markdown=True,
)

if __name__ == "__main__":
    life_valuation_agent.print_response(
        "What did the FY2024 experience studies say about smoker mortality "
        "on term life India, and what did they recommend?",
        stream=True,
    )

Builds a Chroma index with embedding calls at import; run it in Colab. Open the chapter in Colab to run it with your own free Gemini key.