Skip to main content

Interceptors

Lifecycle interceptors and callbacks observe or validate agent runs around model and tool invocations. Python exposes BaseInterceptor (with before, before_llm, after_llm, before_tool, after_tool, on_error, and on_stream_chunk), Rust exposes LifecycleObserver, and TypeScript, Go, C#, and Java expose a lifecycle callback receiving {event, context, chunk}.

from ish import Agent, BaseInterceptor, LifecycleContext


class AuditTools(BaseInterceptor):
async def before_tool(self, ctx: LifecycleContext) -> None:
print(f"Calling {ctx.entity_name}")


agent = Agent(
name="audited_agent",
model="gpt-4o-mini",
interceptors=[AuditTools()],
)

Lifecycle hook registration​

Shared built-in primitives​

Across SDKs, core guardrail and telemetry logic lives in Rust-backed primitives:

  • Metrics: MetricsInterceptor (Python, TypeScript, Go, C#, Java) and MetricsAccumulator (Rust)
  • Guardrails: GuardrailChecker (all 6 SDKs) plus GuardrailInterceptor (Python)
  • Cycle detection: CycleDetector (all 6 SDKs) plus CycleDetectionInterceptor (Python)
  • OpenTelemetry: OTelInterceptor and OTelGenAIInterceptor (Python, TypeScript, Go, C#, Java) and build_otlp_span_json (Rust)
  • Python convenience adapters: PlanningInterceptor and ContextOffloadInterceptor are Python-facing helpers over shared Rust-backed planning and context-offload behavior; the other SDKs expose those capabilities through their native agent/context APIs.

See Lifecycle hooks, Guardrails, and Observability for full examples across all six languages.