Skip to main content
Token budgeting keeps retrieved context within each model’s window so agents never overflow the context limit.
The user asks a knowledge-heavy question; token budgeting trims retrieved chunks so the prompt stays within the model window.

How It Works

Async enforcement (1.6.88+): ExecutionConfig(max_budget=...) is now enforced on async agent runs (astart()) as well as sync runs. BudgetExceededError is raised on both paths when the cap is exceeded. Gateway bots also honour the budget.

Quick Start

1

Agent with Knowledge

Knowledge retrieval applies token budgets automatically when indexing and retrieving.
2

Long-Context Model (gpt-5)

Pass a model name and the window resolves automatically — gpt-5 unlocks the full 1M context.
3

Direct Budget Control

Use TokenBudget when building custom RAG pipelines.

Configuration

TokenBudget Options

Model Context Windows

RAG context-window values are sourced from a single canonical MODEL_LIMITS table shared with the rest of the SDK; RAG-only extras (Mistral, Llama, DeepSeek, Cohere, o1 variants, gpt-4-32k) are layered on top. Values here match get_model_context_window() exactly — pass any model name and get the same window everywhere.
As of PraisonAI PR #3796, the shared context-window lookup consults litellm’s model_cost registry first, so any litellm-known model (Mistral, DeepSeek, xAI, Qwen, Bedrock, Azure, OpenRouter, versioned OpenAI/Anthropic/Google ids) resolves to its true window even when it is not in the table below. The static table is the offline fallback. See How the context window is resolved for the full order.
Unknown or versioned model names resolve via partial match (e.g. gpt-4o-2024-05-13gpt-4o). The default 128,000 tokens only applies when both litellm and the static partial-match miss.

Budget Enforcement

Implement BudgetEnforcerProtocol for priority-based or custom selection strategies.

CLI Usage


Best Practices

Leave enough headroom for complete answers — undersized reserved_response truncates outputs mid-sentence.
Multi-turn conversations consume tokens quickly; increase reserved_history or enable context compaction on long sessions.
Different models have different context windows — always pass the actual model name so dynamic_budget() calculates correctly.
Run praisonai knowledge index ./docs --verbose to see chunk counts and token estimates before production retrieval.

Thinking Budgets

Extended reasoning token limits

Token Usage Protocol

Track token consumption across runs