openclaw - 💡(How to fix) Fix [Feature Request]: Plugin hook for model dispatch completions (for telemetry integrations) [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#50015Fetched 2026-04-08 01:00:16
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

OpenClaw's gateway dispatches model calls through compiled TypeScript internals. There is currently no plugin surface or event hook that fires after a model dispatch completion, which prevents third-party telemetry systems from receiving real-time token usage data.

Root Cause

OpenClaw's gateway dispatches model calls through compiled TypeScript internals. There is currently no plugin surface or event hook that fires after a model dispatch completion, which prevents third-party telemetry systems from receiving real-time token usage data.

Fix Action

Fix / Workaround

OpenClaw's gateway dispatches model calls through compiled TypeScript internals. There is currently no plugin surface or event hook that fires after a model dispatch completion, which prevents third-party telemetry systems from receiving real-time token usage data.

We're running Bonfire — a token telemetry and cost governance layer — alongside OpenClaw. Bonfire has a fully implemented hook (token_hook.record_route_event()) that expects to receive per-dispatch completions in real time. Without a platform hook, we're forced to poll session JSONL files every 5 minutes as a workaround, which introduces lag and prevents real-time cost governance.

A plugin hook or event that fires after each model dispatch completion, providing:

  • agent_id
  • session_id
  • model (selected model identifier)
  • usage (prompt_tokens, completion_tokens, total_tokens)
  • latency_ms
  • status
RAW_BUFFERClick to expand / collapse

Summary

OpenClaw's gateway dispatches model calls through compiled TypeScript internals. There is currently no plugin surface or event hook that fires after a model dispatch completion, which prevents third-party telemetry systems from receiving real-time token usage data.

Use Case

We're running Bonfire — a token telemetry and cost governance layer — alongside OpenClaw. Bonfire has a fully implemented hook (token_hook.record_route_event()) that expects to receive per-dispatch completions in real time. Without a platform hook, we're forced to poll session JSONL files every 5 minutes as a workaround, which introduces lag and prevents real-time cost governance.

What We'd Need

A plugin hook or event that fires after each model dispatch completion, providing:

  • agent_id
  • session_id
  • model (selected model identifier)
  • usage (prompt_tokens, completion_tokens, total_tokens)
  • latency_ms
  • status

This could be a plugin lifecycle event (e.g., onModelDispatchComplete), a registered middleware callback, or an event emitter that plugins can subscribe to.

Current Workaround

Polling ~/.openclaw/agents/*/sessions/*.jsonl every 5 minutes and extracting token usage from session history. Works but is not real-time and creates coupling to session file format.

Impact

Without this hook, any real-time token governance, cost alerting, or model efficiency tooling built on OpenClaw must resort to polling. This limits the value of the existing plugin surface for observability use cases.

Environment

  • OpenClaw: 2026.3.13
  • OS: macOS 14 (Darwin 23.6.0)

Filed by Acme Agent Supply — building reliability and governance tooling for OpenClaw operators.

extent analysis

Fix Plan

To address the issue, we will introduce a new plugin hook onModelDispatchComplete that fires after each model dispatch completion. This hook will provide the required information, including agent_id, session_id, model, usage, latency_ms, and status.

Implementation Steps

  • Update the OpenClaw gateway to emit an onModelDispatchComplete event after each model dispatch completion.
  • Modify the plugin system to allow registration of callbacks for the onModelDispatchComplete event.
  • Implement the token_hook.record_route_event() function in the Bonfire plugin to handle the onModelDispatchComplete event and record token usage data in real-time.

Example Code

// OpenClaw gateway
interface ModelDispatchCompleteEvent {
  agentId: string;
  sessionId: string;
  model: string;
  usage: {
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
  };
  latencyMs: number;
  status: string;
}

class OpenClawGateway {
  private plugins: any[];

  // ...

  onModelDispatchComplete(event: ModelDispatchCompleteEvent) {
    this.plugins.forEach((plugin) => {
      if (plugin.onModelDispatchComplete) {
        plugin.onModelDispatchComplete(event);
      }
    });
  }

  // ...
}

// Bonfire plugin
class BonfirePlugin {
  onModelDispatchComplete(event: ModelDispatchCompleteEvent) {
    token_hook.recordRouteEvent({
      agentId: event.agentId,
      sessionId: event.sessionId,
      model: event.model,
      usage: event.usage,
      latencyMs: event.latencyMs,
      status: event.status,
    });
  }
}

Verification

To verify that the fix worked, you can:

  • Register the Bonfire plugin with the OpenClaw gateway.
  • Dispatch a model call through the OpenClaw gateway.
  • Verify that the token_hook.recordRouteEvent() function is called with the correct data after the model dispatch completion.

Extra Tips

  • Ensure that the onModelDispatchComplete event is properly documented and exposed through the OpenClaw plugin API.
  • Consider adding error handling and logging mechanisms to the onModelDispatchComplete event handling code.
  • Test the fix thoroughly to ensure that it works correctly in different scenarios and edge cases.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING