> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Context Manager • AI Agent SDK

> ContextManager: Unified facade for context management.

# ContextManager

> Defined in the [**manager**](../modules/manager) module.

<Badge color="blue">AI Agent</Badge>

Unified facade for context management.

Orchestrates budgeting, composition, optimization, and monitoring.
Provides hooks for exact LLM boundary snapshots.
Tracks optimization history for debugging.

## Constructor

<ParamField query="model" type="str" required={false} default="'gpt-4o-mini'">
  No description available.
</ParamField>

<ParamField query="config" type="Optional" required={false}>
  No description available.
</ParamField>

<ParamField query="session_id" type="str" required={false} default="''">
  No description available.
</ParamField>

<ParamField query="agent_name" type="str" required={false} default="''">
  No description available.
</ParamField>

<ParamField query="session_cache" type="Optional" required={false}>
  No description available.
</ParamField>

<ParamField query="llm_summarize_fn" type="Optional" required={false}>
  No description available.
</ParamField>

## Methods

<CardGroup cols={2}>
  <Card title="process()" icon="function" href="../functions/ContextManager-process">
    Process messages through the context pipeline.
  </Card>

  <Card title="capture_llm_boundary()" icon="function" href="../functions/ContextManager-capture_llm_boundary">
    Capture exact state at LLM call boundary.
  </Card>

  <Card title="register_snapshot_callback()" icon="function" href="../functions/ContextManager-register_snapshot_callback">
    Register a callback for LLM boundary snapshots.
  </Card>

  <Card title="get_last_snapshot_hook()" icon="function" href="../functions/ContextManager-get_last_snapshot_hook">
    Get the last LLM boundary snapshot.
  </Card>

  <Card title="estimate_tokens()" icon="function" href="../functions/ContextManager-estimate_tokens">
    Estimate tokens with optional validation.
  </Card>

  <Card title="get_tool_budget()" icon="function" href="../functions/ContextManager-get_tool_budget">
    Get token budget for a specific tool.
  </Card>

  <Card title="set_tool_budget()" icon="function" href="../functions/ContextManager-set_tool_budget">
    Set token budget for a specific tool.
  </Card>

  <Card title="truncate_tool_output()" icon="function" href="../functions/ContextManager-truncate_tool_output">
    Truncate tool output according to its budget.
  </Card>

  <Card title="get_history()" icon="function" href="../functions/ContextManager-get_history">
    Get optimization history.
  </Card>

  <Card title="get_stats()" icon="function" href="../functions/ContextManager-get_stats">
    Get current context statistics.
  </Card>

  <Card title="emergency_truncate()" icon="function" href="../functions/ContextManager-emergency_truncate">
    Emergency truncation when optimization isn't enough.
  </Card>

  <Card title="get_resolved_config()" icon="function" href="../functions/ContextManager-get_resolved_config">
    Get the fully resolved configuration with source info.
  </Card>

  <Card title="reset()" icon="function" href="../functions/ContextManager-reset">
    Reset manager state.
  </Card>
</CardGroup>

## Usage

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
manager = ContextManager(model="gpt-4o")
    
    # Process messages before LLM call
    result = manager.process(
        messages=messages,
        system_prompt=system_prompt,
        tools=tools,
    )
    
    # Get optimized messages
    optimized_messages = result["messages"]
    
    # Check if optimization occurred
    if result["optimized"]:
        print(f"Saved {result['tokens_saved']} tokens")
```

## Source

<Card title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-agents/praisonaiagents/context/manager.py#L292">
  `praisonaiagents/context/manager.py` at line 292
</Card>

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Context Concept" icon="layer-group" href="/docs/concepts/context" />

  <Card title="Context Management" icon="sliders" href="/docs/features/context-management" />

  <Card title="Context Strategies" icon="diagram-project" href="/docs/features/context-strategies" />
</CardGroup>
