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}.
- Python
- TypeScript
- Rust
- Go
- C#
- Java
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()],
)
import { Agent } from "ish-harness";
const agent = new Agent({
name: "audited_agent",
model: { provider: "openai_chat", name: "gpt-4o-mini" },
lifecycle: ({ event, context }) => {
if (event === "before_tool") {
console.log(`Calling ${context.entity_name}`);
}
},
});
use std::sync::Arc;
use async_trait::async_trait;
use ish_harness::interceptors::{LifecycleContext, LifecycleEvent, LifecycleObserver};
use ish_harness::{Agent, OpenAIModel, RunnerConfig};
struct AuditTools;
#[async_trait]
impl LifecycleObserver for AuditTools {
async fn dispatch(
&self,
event: LifecycleEvent,
context: &mut LifecycleContext,
_chunk: Option<&str>,
) -> Result<(), String> {
if event == LifecycleEvent::BeforeTool {
println!("Calling {}", context.entity_name);
}
Ok(())
}
}
let config = RunnerConfig {
lifecycle_observer: Some(Arc::new(AuditTools)),
..RunnerConfig::default()
};
agent := ish.NewAgent(client, ish.AgentRunRequest{
AgentName: "audited_agent",
Model: &ish.AgentModel{Provider: "openai_chat", Name: "gpt-4o-mini"},
Lifecycle: func(request map[string]any) (any, bool, error) {
if request["event"] == "before_tool" {
ctxMap, _ := request["context"].(map[string]any)
fmt.Println("Calling", ctxMap["entity_name"])
}
return nil, false, nil
},
})
using Ish.Harness;
var request = new AgentRunRequest
{
AgentName = "audited_agent",
Model = new AgentModel { Provider = "openai_chat", Name = "gpt-4o-mini" },
UserPrompt = "Run diagnostics.",
Lifecycle = req =>
{
if (req.GetProperty("event").GetString() == "before_tool")
{
var tool = req.GetProperty("context").GetProperty("entity_name").GetString();
Console.WriteLine($"Calling {tool}");
}
return null;
},
};
import io.github.ishharness.IshClient;
var request = new IshClient.AgentRequest("gpt-4o-mini", "Assist concisely.", "Run diagnostics.")
.provider("openai_chat")
.agentName("audited_agent")
.lifecycle(req -> {
if ("before_tool".equals(req.path("event").asText())) {
System.out.println("Calling " + req.path("context").path("entity_name").asText());
}
return null;
});
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) andMetricsAccumulator(Rust) - Guardrails:
GuardrailChecker(all 6 SDKs) plusGuardrailInterceptor(Python) - Cycle detection:
CycleDetector(all 6 SDKs) plusCycleDetectionInterceptor(Python) - OpenTelemetry:
OTelInterceptorandOTelGenAIInterceptor(Python, TypeScript, Go, C#, Java) andbuild_otlp_span_json(Rust) - Python convenience adapters:
PlanningInterceptorandContextOffloadInterceptorare 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.