Skip to main content

Agent Hooks

Lifecycle hooks let you tap into key moments of an agent run — log inputs, audit tool calls, and handle errors.

Loop Hooks

Loop hooks fire on every LLM roundtrip inside the agent loop. Use them to track tokens, log messages, and auto-stop when costs get too high.

Cancellation

Use a standard AbortController to cancel a running agent. This is useful for enforcing hard timeouts on long-running runs.

Follow-up Suggestions

Generate suggested follow-up prompts after an agent run. Pass true for defaults, or customize the count and model.

Agent Serialization

Save an agent’s configuration to JSON and restore it later — useful for persistence, versioning, and sharing configs across services.

Checkpointing

Enable checkpointing to save agent state after each tool roundtrip. Supports crash recovery and rollback.
With persistent storage for production use:

Dependencies & Runtime Injection

Inject static values or async resolvers into the agent context. Use {key} template syntax in instructions for dynamic interpolation, and override per-run.

Context Compaction

Automatically compress conversation history when it approaches the model’s context limit. Choose trim, summarize, or hybrid strategies.
The three strategies:
  • trim — drops oldest non-system messages to fit the token budget
  • summarize — condenses older messages into a summary using a cheaper model
  • hybrid — trims first, then summarizes the remaining middle section if still over budget

Dynamic Tools

Resolve tools at runtime based on user context, or mutate the tool set programmatically at any point.
Programmatic tool management:

Event Bus

Subscribe to lifecycle events across agents. Useful for logging, metrics, cost tracking, and custom integrations.

Compression

Enable automatic compression of verbose tool results to reduce token usage and cost. The agent still receives the full semantic content, but large payloads are summarized before being added to the context window.