Metadata-Version: 2.5
Name: recallic-pydantic-ai
Version: 0.1.0
Summary: Recallic as long-term memory for Pydantic AI agents: self-hosted, no LLM calls for memory, says when it has no record.
Project-URL: Homepage, https://www.recallic.com
Project-URL: Source, https://github.com/Recallic/Recallic/tree/main/integrations/pydantic-ai
License-Expression: Apache-2.0
License-File: LICENSE
Requires-Python: >=3.11
Requires-Dist: pydantic-ai-slim<3,>=2.77
Requires-Dist: recallic>=0.3.0
Description-Content-Type: text/markdown

# recallic-pydantic-ai: Recallic memory for Pydantic AI

Website: [recallic.com](https://www.recallic.com)

Long-term memory for [Pydantic AI](https://ai.pydantic.dev) agents, backed by a self-hosted
[Recallic](https://github.com/Recallic/Recallic) server. It is a Pydantic AI capability.

* **No LLM calls for memory.** Storing and searching use Recallic's local models.
* **Across runs and conversations.** Each run, what this user said before that bears on their prompt is recalled,
  whatever conversation it was said in.
* **Honest.** When nothing answers it, the model is told there is no record instead of being handed near-misses to
  guess from.
* **Your data stays on your server**, separated per user and per agent.

This package is Apache-2.0. The Recallic server is source-available.

## Install

```bash
pip install recallic-pydantic-ai
```

Run Recallic ([quickstart](../../docs/quickstart.md)) and create a key with role `service`, which acts for each of
your users (or use a user's own key and leave the user out):

```bash
python -m backend.tools.keys create --user my-pydantic-app --role service
```

## Automatic memory: `RecallicMemory`

```python
import os

from pydantic_ai import Agent
from recallic_pydantic_ai import RecallicMemory

memory = RecallicMemory(os.environ["RECALLIC_SERVICE_KEY"], base_url="http://localhost:8000",
                        mode="balanced")                           # or "cautious" / "helpful"
agent = Agent("openai:gpt-5-mini", instructions="You are a helpful assistant.", deps_type=str,
              capabilities=[memory])

agent.run_sync("I moved to Pune in March.", deps="alice")

# days later, a new conversation
print(agent.run_sync("Which city did I move to?", deps="alice").output)   # "Pune."
```

At the start of each run the capability recalls on the user's prompt. It puts the memories in front of the model,
just before the prompt, as a `<user_memories>` block marked as written by the user. Your own instructions are kept.
When nothing answers, it adds a short instruction telling the model there is no record. Then it stores the prompt.
Recall runs once per run, not once per model request in a tool loop.

**Whose memory.** The user comes from the run's `deps`: a string is the user id, otherwise `deps["user_id"]` or
`deps.user_id`. When the deps name no user, the `user_id` field is used. With neither, a `service` key falls back to
the key's own user, which every run would then share. So with a `service` key, always name the user.

| Parameter | Default | What it does |
|---|---|---|
| `api_key` | `RECALLIC_API_KEY` env var | A Recallic key. `service` acts for each user; a user key acts for itself. |
| `base_url` | `http://localhost:8000` | Your Recallic server. |
| `user_id` | `None` | The user when the run's deps name none. |
| `agent_id` | `pydantic-ai` | Recallic agent id; each agent reads back what it wrote (and what any agent shared). |
| `mode` | server's `recall.mode` | `cautious` reads with a stricter reader and declines more (more "I don't know"); `helpful` answers more counting, ordering and "how long between" questions. |
| `limit` | 5 | Memories given to the model per run. |
| `store_user_messages` | `True` | Store the user's prompt each run (after recalling). |
| `tell_model_on_decline` | `True` | When nothing answers, tell the model not to guess about the user's past. |
| `tools` | `False` | Also give the model `recall_memory(query)` and `save_memory(fact)` tools. |

`RecallicMemory` has the default id `recallic_memory`. It also loads from an agent spec:
`Agent.from_spec(spec, custom_capability_types=[RecallicMemory])`, with `- RecallicMemory: {mode: cautious}` in
YAML and the key in `RECALLIC_API_KEY`.

## Tools only: `recallic_toolset()`

When the agent should decide itself when to look things up or save them:

```python
from recallic_pydantic_ai import recallic_toolset

tools = recallic_toolset(os.environ["RECALLIC_SERVICE_KEY"])   # recall_memory(query), save_memory(fact)
agent = Agent("openai:gpt-5-mini", deps_type=str, toolsets=[tools])
agent.run_sync("What's my sister's name?", deps="alice")
```

The tools read the user from the run's deps in the same way.

Notes:

* The memories and the no-record note are capability parts (`RecalledMemoriesPart`, `NoRecordPart`), so they are
  kept in the run's message history and you can find them by type.
* `format_memories(recall_result)` turns any Recallic recall result into plain text, as the `recall_memory` tool
  returns it.
* Only the user's prompts are stored, not the model's replies.
* Needs Recallic server 0.2.5 or newer. Tested with pydantic-ai-slim 2.77.0.

## Tests

Against a running Recallic: `RECALLIC_URL=http://127.0.0.1:8000 RECALLIC_SERVICE_KEY=rx_... pytest tests`.
They run a real `Agent` with a scripted `FunctionModel`, including its tool loop, and one with `TestModel`.

Licence: Apache-2.0.
