Context Management Policy
The context-management policy category governs conversation length, context window usage, and session boundaries. Use it to prevent agents from running unbounded conversations that exhaust context windows or accumulate excessive costs.
Rules
| Rule | Type | Description |
|---|---|---|
max_conversation_turns | integer | Maximum user turns before action |
max_context_utilization_percent | number | Context window fill percentage limit (1-100) |
max_message_count | integer | Total messages in context before action |
warning_threshold_percent | number | Percentage of limits at which to start warning (1-100) |
action_on_exceed | string | "warn" or "block" when limits are exceeded |
max_session_duration_minutes | integer | Maximum session duration in minutes |
Example Policy
{
"name": "Context Window Guard",
"category": "context-management",
"rules": {
"max_conversation_turns": 50,
"max_context_utilization_percent": 85,
"max_message_count": 200,
"warning_threshold_percent": 70,
"action_on_exceed": "warn"
}
}
Enforcement
Context management policies are evaluated during mid-execution governance checks. When the SDK flushes LLM calls or steps to the controlplane, the current conversation state is included and checked against active policies.
- Warning threshold: When conversation metrics reach the warning percentage of any limit, a
WARNresult is returned. The agent continues but the warning appears in the governance timeline. - Limit exceeded: When metrics exceed any limit, the configured action (
warnorblock) is applied. - Block behavior: If
action_on_exceedis"block", the SDK raises aPolicyViolationErrorthat the agent can catch and handle (e.g., summarize and reset context, or end the session).
Use Cases
- Prevent context overflow: Set
max_context_utilization_percent: 85to warn before the context window fills up - Limit conversation length: Set
max_conversation_turns: 50to prevent unbounded conversations - Cost control: Long conversations with many messages consume more tokens per call — limiting message count indirectly controls cost growth
- Session boundaries: Set
max_session_duration_minutes: 60to enforce session time limits
Combining with Other Policies
Context management works alongside other policy categories:
- Budget policies control total token/cost spend; context management controls per-conversation growth
- Safety policies limit total steps; context management specifically tracks conversation turns
- Rate limit policies control request frequency; context management controls conversation depth