Capabilities API¶
Capabilities for pydantic-ai agents.
Wraps summarization processors and context management as pydantic-ai
AbstractCapability instances, removing the need for pydantic-ai-middleware.
Example
SummarizationCapability
dataclass
¶
Bases: AbstractCapability[Any]
Capability that summarizes conversation history when thresholds are reached.
Wraps SummarizationProcessor as a pydantic-ai capability.
Example
Source code in src/pydantic_ai_summarization/capability.py
SlidingWindowCapability
dataclass
¶
Bases: AbstractCapability[Any]
Capability that trims old messages using a sliding window.
Zero-cost alternative to summarization — discards oldest messages.
Example
Source code in src/pydantic_ai_summarization/capability.py
LimitWarnerCapability
dataclass
¶
Bases: AbstractCapability[Any]
Capability that warns the agent when run limits approach.
Injects a warning as a trailing user message when iteration, context window, or total token limits are near.
Example
Source code in src/pydantic_ai_summarization/capability.py
ContextManagerCapability
dataclass
¶
Bases: AbstractCapability[Any]
Full context management capability with token tracking, auto-compression, and tool output truncation.
Replaces ContextManagerMiddleware + pydantic-ai-middleware with a native
pydantic-ai capability. Uses before_model_request for history processing
and after_tool_execute for tool output truncation.
Example
Source code in src/pydantic_ai_summarization/capability.py
| Python | |
|---|---|
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
max_tokens = None
class-attribute
instance-attribute
¶
Max token budget. None = auto-detect from model via genai-prices (default 200K fallback).
keep = ('messages', 0)
class-attribute
instance-attribute
¶
How much of the tail to preserve after compression.
The default keeps nothing beyond the in-flight request — the pending tool returns or the new user prompt the imminent model call is about — so only the summary and that exchange survive.
include_compact_tool = False
class-attribute
instance-attribute
¶
When True, adds a compact_conversation tool so the agent can trigger compression.
compression_count
property
¶
Number of times compression has been triggered.
get_toolset()
¶
Return a toolset with the compact_conversation tool, or None.
Source code in src/pydantic_ai_summarization/capability.py
for_run(ctx)
async
¶
Auto-detect max_tokens from model on first run if not set.
Source code in src/pydantic_ai_summarization/capability.py
request_compact(focus=None)
¶
Request manual compaction on the next model request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
focus
|
str | None
|
Optional focus instructions for the summary. |
None
|
Source code in src/pydantic_ai_summarization/capability.py
compact(messages, focus=None)
async
¶
Directly compact messages. Callable outside agent.run().
Always attempts compression (force=True), matching the contract of
the compact_conversation tool — calling this method should not be a
no-op (issue #30 point #3). Fires on_before_compress /
on_after_compress the same way before_model_request does, so
direct callers observe the same hook contract. Compression count only
increments when a summary was actually produced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[ModelMessage]
|
Message history to compress. |
required |
focus
|
str | None
|
Optional focus instructions for the summary. |
None
|
Returns:
| Type | Description |
|---|---|
list[ModelMessage]
|
Compressed message list (or input unchanged if the summary LLM failed). |
Source code in src/pydantic_ai_summarization/capability.py
before_model_request(ctx, request_context)
async
¶
Track tokens, auto-compress when threshold reached.
The summarization processor is the single decision-maker for whether
and where to compress: this capability just relays its plan to the
on_before_compress / on_after_compress hooks. Manual compaction
(request_compact() / the compact_conversation tool) sets
force=True, so the tool call always compresses rather than being
silently vetoed by the trigger check.
Source code in src/pydantic_ai_summarization/capability.py
after_tool_execute(ctx, *, call, tool_def, args, result)
async
¶
Truncate large tool outputs.
Only plain str results and a ToolReturn's textual
return_value are truncated; a ToolReturn is never stringified,
since its content may carry BinaryContent that str() would
inline as a multi-MB blob.