Skip to main content

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

RuleTypeDescription
max_conversation_turnsintegerMaximum user turns before action
max_context_utilization_percentnumberContext window fill percentage limit (1-100)
max_message_countintegerTotal messages in context before action
warning_threshold_percentnumberPercentage of limits at which to start warning (1-100)
action_on_exceedstring"warn" or "block" when limits are exceeded
max_session_duration_minutesintegerMaximum 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 WARN result is returned. The agent continues but the warning appears in the governance timeline.
  • Limit exceeded: When metrics exceed any limit, the configured action (warn or block) is applied.
  • Block behavior: If action_on_exceed is "block", the SDK raises a PolicyViolationError that 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: 85 to warn before the context window fills up
  • Limit conversation length: Set max_conversation_turns: 50 to 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: 60 to 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