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
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:
Get partial context
Read whatβs been accumulated for a session before the threshold is reached:Composing with RagPipeline
The most common pattern β attach the summarizer to aRagPipeline so summaries are automatically embedded and upserted:
Constructor
SessionSummarizer(llm, threshold=10, prompt=None, on_summary=None)
Callable[[str], str]
required
Any synchronous LLM callable. Receives the formatted prompt and must return the summary as a plain string.
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.
str | None
default:"None"
Custom prompt template. Must contain
{actions} as a placeholder. Uses the built-in default if not provided.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.