Skip to main content

Pydantic AI integration for Hindsight - persistent memory tools for AI agents

Project description

hindsight-pydantic-ai

Persistent memory tools for Pydantic AI agents via Hindsight. Give your agents long-term memory with retain, recall, and reflect — all async-native with no thread-pool hacks.

Features

  • Async-Native Tools - Uses Pydantic AI's async tool interface directly (aretain, arecall, areflect)
  • Memory Instructions - Auto-inject relevant memories into every agent run via instructions=[...]
  • Three Memory Tools - Retain (store), Recall (search), Reflect (synthesize) — include any combination
  • Simple Configuration - Configure once globally, or pass a client directly
  • Lightweight - Depends on pydantic-ai-slim to avoid pulling in all model providers

Installation

pip install hindsight-pydantic-ai

Quick Start

from hindsight_client import Hindsight
from hindsight_pydantic_ai import create_hindsight_tools, memory_instructions
from pydantic_ai import Agent

client = Hindsight(base_url="http://localhost:8888")

agent = Agent(
    "openai:gpt-4o",
    tools=create_hindsight_tools(client=client, bank_id="user-123"),
    instructions=[memory_instructions(client=client, bank_id="user-123")],
)

result = await agent.run("What do you remember about my preferences?")
print(result.output)

The agent now has three tools it can call:

  • hindsight_retain — Store information to long-term memory
  • hindsight_recall — Search long-term memory for relevant facts
  • hindsight_reflect — Synthesize a reasoned answer from memories

The memory_instructions callable automatically recalls relevant memories and injects them into the system prompt on every run.

Tools Only (No Auto-Injection)

If you want the agent to decide when to use memory (rather than always injecting context):

agent = Agent(
    "openai:gpt-4o",
    tools=create_hindsight_tools(client=client, bank_id="user-123"),
)

Instructions Only (No Tools)

If you just want memories auto-injected without giving the agent explicit memory tools:

agent = Agent(
    "openai:gpt-4o",
    instructions=[memory_instructions(client=client, bank_id="user-123")],
)

Selecting Tools

Include only the tools you need:

tools = create_hindsight_tools(
    client=client,
    bank_id="user-123",
    include_retain=True,
    include_recall=True,
    include_reflect=False,  # Omit reflect
)

Global Configuration

Instead of passing a client to every call, configure once:

from hindsight_pydantic_ai import configure, create_hindsight_tools

configure(
    hindsight_api_url="http://localhost:8888",
    api_key="your-api-key",       # Or set HINDSIGHT_API_KEY env var
    budget="mid",                  # Recall budget: low/mid/high
    max_tokens=4096,               # Max tokens for recall results
    tags=["env:prod"],             # Tags for stored memories
    recall_tags=["scope:global"],  # Tags to filter recall
    recall_tags_match="any",       # Tag match mode: any/all/any_strict/all_strict
)

# Now create tools without passing client — uses global config
tools = create_hindsight_tools(bank_id="user-123")

Per-Tool Overrides

Constructor arguments override global configuration:

tools = create_hindsight_tools(
    bank_id="user-123",
    budget="high",             # Override global budget
    max_tokens=8192,           # Override global max_tokens
    tags=["session:abc"],      # Override global tags
)

Memory Instructions Options

Customize what memories get injected and how:

instructions_fn = memory_instructions(
    client=client,
    bank_id="user-123",
    query="relevant context about the user",  # What to search for
    budget="low",                              # Keep it fast
    max_results=5,                             # Limit injected memories
    max_tokens=4096,                           # Max recall tokens
    prefix="Relevant memories:\n",             # Text before the memory list
    tags=["scope:global"],                     # Filter by tags
    tags_match="any",                          # Tag match mode
)

Configuration Reference

create_hindsight_tools()

Parameter Default Description
bank_id required Hindsight memory bank ID
client None Pre-configured Hindsight client
hindsight_api_url None API URL (used if no client provided)
api_key None API key (used if no client provided)
budget "mid" Recall/reflect budget level (low/mid/high)
max_tokens 4096 Maximum tokens for recall results
tags None Tags applied when storing memories
recall_tags None Tags to filter when searching
recall_tags_match "any" Tag matching mode
include_retain True Include the retain (store) tool
include_recall True Include the recall (search) tool
include_reflect True Include the reflect (synthesize) tool

memory_instructions()

Parameter Default Description
bank_id required Hindsight memory bank ID
client None Pre-configured Hindsight client
hindsight_api_url None API URL (used if no client provided)
api_key None API key (used if no client provided)
query "relevant context about the user" Recall query for memory injection
budget "low" Recall budget level
max_results 5 Maximum memories to inject
max_tokens 4096 Maximum tokens for recall results
prefix "Relevant memories:\n" Text prepended before memory list
tags None Tags to filter recall results
tags_match "any" Tag matching mode

configure()

Parameter Default Description
hindsight_api_url Production API Hindsight API URL
api_key HINDSIGHT_API_KEY env API key for authentication
budget "mid" Default recall budget level
max_tokens 4096 Default max tokens for recall
tags None Default tags for retain operations
recall_tags None Default tags to filter recall
recall_tags_match "any" Default tag matching mode
verbose False Enable verbose logging

Requirements

  • Python >= 3.10
  • pydantic-ai-slim >= 1.0.0
  • hindsight-client >= 0.4.0
  • A running Hindsight API server

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hindsight_pydantic_ai-0.4.20.tar.gz (90.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hindsight_pydantic_ai-0.4.20-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file hindsight_pydantic_ai-0.4.20.tar.gz.

File metadata

  • Download URL: hindsight_pydantic_ai-0.4.20.tar.gz
  • Upload date:
  • Size: 90.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hindsight_pydantic_ai-0.4.20.tar.gz
Algorithm Hash digest
SHA256 f0a1ca86ec7c3db7f15cc65ee9ff797a8f73a3998ad2230836eaea58fa702b91
MD5 b36e3b85c7caee0024bed32919a63536
BLAKE2b-256 2276c5cdc496cbad7222b2ec0ce4ea762df1bab3a07fb896fbb9c08332d94db2

See more details on using hashes here.

Provenance

The following attestation bundles were made for hindsight_pydantic_ai-0.4.20.tar.gz:

Publisher: release-integration.yml on vectorize-io/hindsight

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hindsight_pydantic_ai-0.4.20-py3-none-any.whl.

File metadata

File hashes

Hashes for hindsight_pydantic_ai-0.4.20-py3-none-any.whl
Algorithm Hash digest
SHA256 08fa7e2ac184c317215b40aa0173ee593d70d381dc48f7a55e3b28fb5f604baa
MD5 27544c0c5d495ebd2d909f68ff227c64
BLAKE2b-256 5638ca2a72ffe159a8a68675898473ff0970c9ba42d0de458e61ab9210999850

See more details on using hashes here.

Provenance

The following attestation bundles were made for hindsight_pydantic_ai-0.4.20-py3-none-any.whl:

Publisher: release-integration.yml on vectorize-io/hindsight

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page