Skip to main content

Pydantic AI + Memanto

Pydantic AI Pydantic AI A Pydantic AI agent has no memory of its own. Every run starts fresh, with nothing carried over from earlier conversations. memanto-pydantic-ai connects a Pydantic AI agent to Memanto in two complementary ways: relevant memories are recalled and added to the agent’s instructions automatically on every run, and three tools let the model save a structured memory, search stored memories, or get a synthesized answer from everything it’s learned.

Automatic recall

memory_instructions recalls the memories most relevant to the user’s prompt before each run and adds them to the agent’s instructions, so the agent uses its memory even when the model doesn’t think to look.

Works with Pydantic AI's own primitives

The tools drop into Agent(tools=...) and the recall into Agent(instructions=...). No custom agent class, no extra wiring.

Typed memories, not transcripts

Saved memories go through Memanto’s typed model (13 categories, including fact, preference, decision, goal, and learning), not raw conversation logs.

Fails safe

Invalid tool calls go back to the model as retries, a memory outage never takes your agent down, and misconfiguration fails at startup with a clear error.

How It Works

There’s no Memanto server to run: Pydantic AI and Memanto are both Python, so everything calls Memanto’s client in-process. Memory lives in a Memanto agent, so you can read, correct, and audit what your Pydantic AI agent knows with the CLI or the web UI, same as any other integration.

Prerequisites

  • Python 3.10+
  • pydantic-ai-slim 2.0+ (installed automatically) plus the extra for your model provider, or the full pydantic-ai package
  • Memanto 0.2.21+ (installed automatically) and a Moorcheh API key (or an on-prem Memanto backend)
  • Credentials for the model your agent uses (the example below uses OpenAI gpt-4o, so set OPENAI_API_KEY)

Install

Swap [openai] for your provider’s extra ([anthropic], [google], …), or install the full pydantic-ai package.

Use it

  • MemantoSetup.setup creates the Memanto agent on first use (and reuses it after that), then activates a session. Sessions renew automatically while in use.
  • memory_instructions recalls the memories most relevant to each run’s prompt and adds them to the agent’s instructions.
  • create_memanto_tools binds the client and agent id when the tools are created, so the model’s only inputs are the memory content or query it’s reasoning about.
Either piece works on its own: tools only (the model decides when to read memory), or memory_instructions only (read-only, automatic recall).

Automatic recall

memory_instructions turns each run’s user prompt into a recall query and adds the matches to the agent’s instructions, most relevant first:
  • Once per run. Pydantic AI re-evaluates instructions before every model request, so the recall result is cached for the rest of the run instead of repeating it after each tool call.
  • Only current memories. Expired memories are never injected.
  • The prompt is the query. Only text parts of the prompt are used (images and files are skipped), truncated to 1000 characters. If there are no matches, nothing is added.
  • Outages degrade, not fail. If the recall fails (network, auth, backend), a warning is logged and the run continues without injected memories.

Available Tools

Memories saved through memanto_remember are tagged with source pydantic-ai-agent and provenance explicit_statement. Pass include_remember=False (or include_recall / include_answer) to create_memanto_tools to expose a subset, for example for a read-only agent.

Invalid tool calls are retried, not fatal

The limits above are part of each tool’s schema, so the model sees them up front. If it still sends a bad call (an unknown memory type, a confidence of 85 instead of 0.85, a recall limit over 100, an over-long title), Pydantic AI returns the validation error to the model as a retry prompt and the model corrects the call. The agent run keeps going. Network, auth, and backend errors from the tools still raise normally.

Memory per user

A Memanto client holds one session for one agent. To give each end user their own memory, use one Memanto agent per user, each with its own MemantoSetup:
Cache the result per user in a long-running app rather than calling setup on every request. create_memanto_tools and memory_instructions check the client when they’re created: if it has no active session, or its session is for a different agent, they raise a ValueError explaining the fix, instead of failing on the model’s first tool call.
One session per agent. Activating a session invalidates any other session for the same Memanto agent, including one opened by the memanto CLI. Give each running app its own agent_id.

API

Shared memory across integrations

All Memanto integration packages use the same Moorcheh-backed agents when they share an agent_id:

Next Steps