Skip to main content
Policies let you inject logic at each step of agent execution. Add instructions, modify behavior, enforce constraints—all based on runtime context like step count, previous outputs, or external state.

Basic Policy

A policy is a function that receives context and returns modifications:

Policy Context

The ctx dict contains:

Policy Returns

Policies can return:

Use Cases

Rate limiting: Track API calls across steps, pause if limits approached. Guardrails: Check outputs for policy violations, inject correction prompts. Dynamic instructions: Change behavior based on intermediate results. Cost control: Stop execution after a certain number of expensive operations.

Tool Event Callbacks

Monitor tool execution with on_tool_event:

Next steps

Tools

Define the tools policies can control
Last modified on June 30, 2026