Skip to main content

Automatic Conversation Summarization and History Management for Pydantic AI

Project description

Context Management for Pydantic AI

Automatic Conversation Summarization and History Management

PyPI version Python 3.10+ License: MIT CI Pydantic AI

Intelligent Summarization — LLM-powered context compression  •  Sliding Window — zero-cost message trimming  •  Safe Cutoff — preserves tool call pairs


Context Management for Pydantic AI helps your Pydantic AI agents handle long conversations without exceeding model context limits. Choose between intelligent LLM summarization or fast sliding window trimming.

Full framework? Check out Pydantic Deep Agents — complete agent framework with planning, filesystem, subagents, and skills.

Use Cases

What You Want to Build How This Library Helps
Long-Running Agent Automatically compress history when context fills up
Customer Support Bot Preserve key details while discarding routine exchanges
Code Assistant Keep recent code context, summarize older discussions
High-Throughput App Zero-cost sliding window for maximum speed
Cost-Sensitive App Choose between quality (summarization) or free (sliding window)

Installation

pip install summarization-pydantic-ai

Or with uv:

uv add summarization-pydantic-ai

For accurate token counting:

pip install summarization-pydantic-ai[tiktoken]

Quick Start

from pydantic_ai import Agent
from pydantic_ai_summarization import create_summarization_processor

processor = create_summarization_processor(
    trigger=("tokens", 100000),
    keep=("messages", 20),
)

agent = Agent(
    "openai:gpt-4o",
    history_processors=[processor],
)

result = await agent.run("Hello!")

That's it. Your agent now:

  • Monitors conversation size on every turn
  • Summarizes older messages when limits are reached
  • Preserves tool call/response pairs (never breaks them)
  • Keeps recent context intact

Available Processors

Processor LLM Cost Latency Context Preservation
SummarizationProcessor High High Intelligent summary
SlidingWindowProcessor Zero ~0ms Discards old messages

Intelligent Summarization

Uses an LLM to create summaries of older messages:

from pydantic_ai_summarization import create_summarization_processor

processor = create_summarization_processor(
    trigger=("tokens", 100000),  # When to summarize
    keep=("messages", 20),       # What to keep
)

Zero-Cost Sliding Window

Simply discards old messages — no LLM calls:

from pydantic_ai_summarization import create_sliding_window_processor

processor = create_sliding_window_processor(
    trigger=("messages", 100),  # When to trim
    keep=("messages", 50),      # What to keep
)

Trigger Types

Type Example Description
messages ("messages", 50) Trigger when message count exceeds threshold
tokens ("tokens", 100000) Trigger when token count exceeds threshold
fraction ("fraction", 0.8) Trigger at percentage of max_input_tokens

Keep Types

Type Example Description
messages ("messages", 20) Keep last N messages
tokens ("tokens", 10000) Keep last N tokens worth
fraction ("fraction", 0.2) Keep last N% of context

Advanced Configuration

Multiple Triggers

from pydantic_ai_summarization import SummarizationProcessor

processor = SummarizationProcessor(
    model="openai:gpt-4o",
    trigger=[
        ("messages", 50),    # OR 50+ messages
        ("tokens", 100000),  # OR 100k+ tokens
    ],
    keep=("messages", 10),
)

Fraction-Based

processor = SummarizationProcessor(
    model="openai:gpt-4o",
    trigger=("fraction", 0.8),  # 80% of context window
    keep=("fraction", 0.2),     # Keep last 20%
    max_input_tokens=128000,    # GPT-4's context window
)

Custom Token Counter

def my_token_counter(messages):
    return sum(len(str(msg)) for msg in messages) // 4

processor = create_summarization_processor(
    token_counter=my_token_counter,
)

Custom Summary Prompt

processor = create_summarization_processor(
    summary_prompt="""
    Extract key information from this conversation.
    Focus on: decisions made, code written, pending tasks.

    Conversation:
    {messages}
    """,
)

Why Choose This Library?

Feature Description
Two Strategies Intelligent summarization or fast sliding window
Flexible Triggers Message count, token count, or fraction-based
Safe Cutoff Never breaks tool call/response pairs
Custom Counters Bring your own token counting logic
Custom Prompts Control how summaries are generated
Zero Dependencies Only requires pydantic-ai

Related Projects

Package Description
Pydantic Deep Agents Full agent framework (uses this library)
pydantic-ai-backend File storage and Docker sandbox
pydantic-ai-todo Task planning toolset
subagents-pydantic-ai Multi-agent orchestration
pydantic-ai The foundation — agent framework by Pydantic

Contributing

git clone https://github.com/vstorm-co/summarization-pydantic-ai.git
cd summarization-pydantic-ai
make install
make test  # 100% coverage required

License

MIT — see LICENSE

Built with ❤️ by vstorm-co

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

summarization_pydantic_ai-0.0.2.tar.gz (270.6 kB view details)

Uploaded Source

Built Distribution

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

summarization_pydantic_ai-0.0.2-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file summarization_pydantic_ai-0.0.2.tar.gz.

File metadata

File hashes

Hashes for summarization_pydantic_ai-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d8175ab8c19604baebb535df6b4a305353be6f1886445dcfdda41b7355bdca3f
MD5 3b9ff77a278cc09824d0c65019869cf4
BLAKE2b-256 83016be6323b5f6151f418ea58b8285d3c035cf0faf8fa65fa36a29f2526af27

See more details on using hashes here.

Provenance

The following attestation bundles were made for summarization_pydantic_ai-0.0.2.tar.gz:

Publisher: publish.yml on vstorm-co/summarization-pydantic-ai

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

File details

Details for the file summarization_pydantic_ai-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for summarization_pydantic_ai-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 be500d3fd256ab29c1abae7119ac3d21869309c9856374f6bf485bf4aa04fc32
MD5 f2682f838cad872afcbac95a558082ab
BLAKE2b-256 a6d001ceac16050a375d597d5dc35dc6060c332168bc327133bb5559df3aa65f

See more details on using hashes here.

Provenance

The following attestation bundles were made for summarization_pydantic_ai-0.0.2-py3-none-any.whl:

Publisher: publish.yml on vstorm-co/summarization-pydantic-ai

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