Skip to main content
SessionSummarizer (sync) and AsyncSessionSummarizer (async) solve the context-window problem for long user sessions. Instead of accumulating hundreds of raw actions that grow unboundedly, the summarizer condenses them into a compact prose paragraph every N actions β€” keeping your RAG context small and retrieval meaningful.

How it works

Each group of N actions is summarised independently. The on_summary callback fires after each summarisation β€” wire it to your vector store, database, or RagPipeline.

Sync usage


Async usage


Custom prompt

By default the summarizer uses a built-in prompt optimised for RAG pipelines. You can override it with your own β€” just include {actions} as the placeholder:
Default prompt:

Get partial context

Read what’s been accumulated for a session before the threshold is reached:
Useful for including real-time context in a support AI agent response before the full summary fires.

Composing with RagPipeline

The most common pattern β€” attach the summarizer to a RagPipeline so summaries are automatically embedded and upserted:

Constructor

SessionSummarizer(llm, threshold=10, prompt=None, on_summary=None)

llm
Callable[[str], str]
required
Any synchronous LLM callable. Receives the formatted prompt and must return the summary as a plain string.
threshold
int
default:"10"
Number of individual actions (not batches) to accumulate before triggering summarisation. A batch of 3 actions counts as 3 toward the threshold.
prompt
str | None
default:"None"
Custom prompt template. Must contain {actions} as a placeholder. Uses the built-in default if not provided.
on_summary
Callable[[str, str], None] | None
default:"None"
Called after each summarisation with (session_id, summary_text). Can also be set after construction by assigning to summarizer.on_summary.

AsyncSessionSummarizer(llm, threshold=10, prompt=None, on_summary=None)

Same parameters but llm is async (prompt: str) -> str and on_summary can be sync or async.

API reference

Method / PropertyDescription
.add(payload)Receive an actions batch β€” wire to on_actions
.get_context(session_id)Return accumulated (not-yet-summarised) actions as text
.reset(session_id)Clear a session’s history without summarising
.active_sessionsList of session IDs with pending actions
.on_summaryAssignable callback (session_id, summary) -> None