Build Custom AI Observability: New llm_input and llm_output Hooks in OpenClaw Plugins
If you've ever wanted to know exactly what prompts your agent sends to the model—or track token usage in real-time—OpenClaw 2026.2.15 just made it possible. The new llm_input and llm_output hook payloads give plugin developers unprecedented visibility into the LLM request/response cycle.
What's New
OpenClaw's plugin system now exposes two powerful hooks:
llm_input: Fires before each model call, exposing the full prompt/input contextllm_output: Fires after model responses, including usage details (tokens, latency, etc.)
This came from PR #16724, contributed by @SecondThread.
Why This Matters
Before this change, if you wanted to log prompts or track model usage at a granular level, you had limited options. Now you can build:
- Custom logging pipelines — Send every prompt/response to your own observability stack (Datadog, Grafana, custom dashboards)
- Token usage analytics — Track costs per session, per user, or per agent
- Prompt debugging tools — Capture exactly what context the model received when something went wrong
- Custom guardrails — Inspect inputs before they're sent, outputs before they're delivered
- A/B testing infrastructure — Compare model behavior across different prompt strategies
Example: Basic Logging Plugin
Here's a minimal example of capturing these events:
// my-observability-plugin/index.ts
import { definePlugin } from "@openclaw/sdk";
export default definePlugin({
name: "my-observability",
hooks: {
llm_input: async ({ prompt, model, sessionKey }) => {
console.log(`[${sessionKey}] Sending to ${model}:`, prompt.slice(0, 200));
// Send to your logging service
await fetch("https://your-logging-service.com/llm-input", {
method: "POST",
body: JSON.stringify({ model, prompt, sessionKey, timestamp: Date.now() })
});
},
llm_output: async ({ response, usage, model, sessionKey }) => {
console.log(`[${sessionKey}] Response from ${model}:`, {
inputTokens: usage?.inputTokens,
outputTokens: usage?.outputTokens,
latencyMs: usage?.latencyMs
});
// Track usage metrics
await trackUsage(sessionKey, usage);
}
}
});Combining with Other Hooks
These new hooks complement existing plugin capabilities:
| Hook | Purpose |
|---|---|
before_agent_start | Override model/provider before resolution |
llm_input | NEW — Observe/log prompts before model calls |
llm_output | NEW — Observe/log responses and usage after calls |
before_tool_call | Intercept tool execution |
message:received / message:sent | Track inbound/outbound messages |
Real-World Use Cases
1. Cost Attribution
Track token usage per customer/tenant by correlating sessionKey with your user database.
2. Prompt Versioning Log every prompt sent to build a dataset for fine-tuning or prompt optimization.
3. Anomaly Detection Alert when prompts exceed certain lengths or outputs contain unexpected patterns.
4. Compliance Logging For regulated industries, capture a full audit trail of all LLM interactions.
Getting Started
- Create a new plugin in your workspace's
plugins/directory - Implement the
llm_inputand/orllm_outputhooks - Enable your plugin in
openclaw.yaml:
plugins:
- ./plugins/my-observability- Restart your gateway—your hooks will now fire on every LLM call
What's in the Payload
The exact payload structure includes:
llm_input:{ prompt, messages, model, provider, sessionKey, agentId, ... }llm_output:{ response, usage: { inputTokens, outputTokens, latencyMs }, model, provider, sessionKey, ... }
Check the OpenClaw plugin documentation for the full schema.
This is a foundational feature for anyone building production AI systems. Whether you're debugging a misbehaving agent or building enterprise-grade observability, these hooks give you the visibility you need.
Have you built something cool with these hooks? Share it in the OpenClaw Discord!
Comments (0)
No comments yet. Be the first to comment!