openclaw - ✅(Solved) Fix feat: include usage/cost metadata in agent.wait terminal response [2 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

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#49494Fetched 2026-04-08 00:54:44
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Participants
Timeline (top)
cross-referenced ×3commented ×2referenced ×1

The agent.wait WebSocket response currently returns only { status, startedAt, endedAt, error }. External orchestrators like Paperclip need per-run token usage and cost data to enforce budgets and track spend — but this data never reaches the wire despite being computed internally.

Error Message

// agent.ts L790-796 respond(true, { runId, status: snapshot.status, startedAt: snapshot.startedAt, endedAt: snapshot.endedAt, error: snapshot.error, });

Root Cause

  • Paperclip's OpenClaw Gateway adapter (@paperclipai/adapter-openclaw-gateway) already has parseUsage() ready to consume usage, costUsd, provider, and model from response metadata — it just gets undefined today.
  • Paperclip PR #884 fixes the meta extraction priority chain on the consuming side.
  • Paperclip PR #949 (merged) added full billing ledger support including biller vs provider distinction, budget enforcement, and cost UI — all waiting for upstream cost data.
  • Without this, OpenClaw agents in Paperclip show $0 spend and budget enforcement never triggers.

Fix Action

Fix / Workaround

  • Paperclip PR #884 — consuming side meta extraction fix
  • Paperclip PR #949 — billing ledger and cost control plane
  • Paperclip PR #386 — heartbeat cost recording fix (merged in v0.3.1)

PR fix notes

PR #884: feat: openclaw-gateway adapter — include contextUsagePercent in run result metadata (ALE-1129)

Description (problem / solution / changelog)

Summary

Resolves ALE-1129.

The OpenClaw gateway cloud adapter now correctly surfaces context window usage (percentage, tokens used, and context window size) in the run result's usage field.

Changes

packages/adapters/openclaw-gateway/src/server/execute.ts

1. Extend parseUsage() with context window fields

  • Extracts contextUsagePercent, contextTokensUsed, contextTokensMax from the raw usage record
  • Derives contextUsagePercent from token counts when not explicitly provided: Math.round(contextTokensUsed / contextTokensMax * 100)
  • These are forwarded into AdapterExecutionResult.usage which Paperclip stores and surfaces in the live-agents dashboard

2. Fix meta lookup to also cover agent.wait response Previously, result.meta.agentMeta was only read from acceptedPayload (the initial agent response). For most runs, OpenClaw sends the final result metadata inside the agent.wait terminal snapshot. The code now checks both:

acceptedPayload.result.meta  → acceptedPayload.meta
  → latestResultPayload.result.meta  → latestResultPayload.meta

3. Use lastCallUsage.input as contextTokensUsed OpenClaw's agentMeta.usage.input is the accumulated sum of input tokens across all tool-use loops in a run. This overstates the actual context window consumption. agentMeta.lastCallUsage.input reflects only the last API call — the true context snapshot at end of run. When lastCallUsage.input is available and no explicit contextTokensUsed is provided, it is used as the preferred source.

4. Merge all context fields into rawUsage Explicit contextUsagePercent / contextTokensUsed / contextTokensMax at both agentMeta and meta levels are merged into the rawUsage object so they override the accumulated token fallbacks.

server/src/__tests__/openclaw-gateway-adapter.test.ts

Added a new test that:

  • Creates a mock gateway returning agentMeta.lastCallUsage (with input: 35000) alongside agentMeta.usage.input (accumulated: 50000) in the agent.wait response
  • Asserts result.usage.contextTokensUsed === 35000 (lastCallUsage wins)
  • Asserts result.usage.inputTokens === 50000 (accumulated usage unchanged)

Test results

Test Files  34 passed (34)
     Tests  144 passed (144)

Changed files

  • packages/adapters/openclaw-gateway/src/server/execute.ts (modified, +66/-3)
  • server/src/__tests__/openclaw-gateway-adapter.test.ts (modified, +101/-0)

PR #949: feat(costs): add billing, quota, and budget control plane

Description (problem / solution / changelog)

Summary

  • add billing, finance, quota, and budget control plane work
  • add agent, project, and company budget enforcement plus budget approvals
  • add /costs UI for providers, billers, finance, quotas, and budgets

Verification

Changed files

  • cli/src/__tests__/company-delete.test.ts (modified, +2/-0)
  • doc/plans/2026-03-14-billing-ledger-and-reporting.md (added, +468/-0)
  • doc/plans/2026-03-14-budget-policies-and-enforcement.md (added, +611/-0)
  • packages/adapter-utils/src/billing.test.ts (added, +28/-0)
  • packages/adapter-utils/src/billing.ts (added, +20/-0)
  • packages/adapter-utils/src/index.ts (modified, +3/-0)
  • packages/adapter-utils/src/types.ts (modified, +47/-1)
  • packages/adapters/claude-local/package.json (modified, +3/-1)
  • packages/adapters/claude-local/src/cli/quota-probe.ts (added, +124/-0)
  • packages/adapters/claude-local/src/server/execute.ts (modified, +7/-1)
  • packages/adapters/claude-local/src/server/index.ts (modified, +12/-0)
  • packages/adapters/claude-local/src/server/quota.ts (added, +531/-0)
  • packages/adapters/codex-local/package.json (modified, +2/-1)
  • packages/adapters/codex-local/src/cli/quota-probe.ts (added, +112/-0)
  • packages/adapters/codex-local/src/server/execute.ts (modified, +15/-3)
  • packages/adapters/codex-local/src/server/index.ts (modified, +11/-0)
  • packages/adapters/codex-local/src/server/quota.ts (added, +556/-0)
  • packages/adapters/cursor-local/src/server/execute.ts (modified, +20/-3)
  • packages/adapters/gemini-local/src/server/execute.ts (modified, +8/-2)
  • packages/adapters/opencode-local/src/server/execute.ts (modified, +6/-1)
  • packages/adapters/pi-local/src/server/execute.ts (modified, +6/-1)
  • packages/db/package.json (modified, +1/-0)
  • packages/db/src/migration-runtime.ts (modified, +56/-6)
  • packages/db/src/migration-status.ts (modified, +17/-1)
  • packages/db/src/migrations/0031_zippy_magma.sql (added, +51/-0)
  • packages/db/src/migrations/0032_pretty_doctor_octopus.sql (added, +102/-0)
  • packages/db/src/migrations/0033_shiny_black_tarantula.sql (added, +2/-0)
  • packages/db/src/migrations/0034_fat_dormammu.sql (added, +2/-0)
  • packages/db/src/migrations/meta/0031_snapshot.json (added, +7242/-0)
  • packages/db/src/migrations/meta/0032_snapshot.json (added, +7733/-0)
  • packages/db/src/migrations/meta/0033_snapshot.json (added, +9038/-0)
  • packages/db/src/migrations/meta/0034_snapshot.json (added, +9039/-0)
  • packages/db/src/migrations/meta/_journal.json (modified, +28/-0)
  • packages/db/src/schema/agents.ts (modified, +2/-0)
  • packages/db/src/schema/budget_incidents.ts (added, +42/-0)
  • packages/db/src/schema/budget_policies.ts (added, +43/-0)
  • packages/db/src/schema/companies.ts (modified, +2/-0)
  • packages/db/src/schema/cost_events.ts (modified, +19/-0)
  • packages/db/src/schema/finance_events.ts (added, +67/-0)
  • packages/db/src/schema/index.ts (modified, +3/-0)
  • packages/db/src/schema/projects.ts (modified, +2/-0)
  • packages/shared/src/constants.ts (modified, +71/-1)
  • packages/shared/src/index.ts (modified, +45/-0)
  • packages/shared/src/types/agent.ts (modified, +3/-0)
  • packages/shared/src/types/budget.ts (added, +99/-0)
  • packages/shared/src/types/company.ts (modified, +3/-1)
  • packages/shared/src/types/cost.ts (modified, +77/-0)
  • packages/shared/src/types/dashboard.ts (modified, +6/-0)
  • packages/shared/src/types/finance.ts (added, +60/-0)
  • packages/shared/src/types/index.ts (modified, +11/-1)
  • packages/shared/src/types/project.ts (modified, +3/-1)
  • packages/shared/src/types/quota.ts (added, +26/-0)
  • packages/shared/src/validators/budget.ts (added, +37/-0)
  • packages/shared/src/validators/cost.ts (modified, +9/-1)
  • packages/shared/src/validators/finance.ts (added, +34/-0)
  • packages/shared/src/validators/index.ts (modified, +12/-0)
  • scripts/dev-runner.mjs (modified, +48/-10)
  • server/package.json (modified, +1/-1)
  • server/src/__tests__/budgets-service.test.ts (added, +311/-0)
  • server/src/__tests__/companies-route-path-guard.test.ts (modified, +3/-0)
  • server/src/__tests__/costs-service.test.ts (added, +226/-0)
  • server/src/__tests__/monthly-spend-service.test.ts (added, +90/-0)
  • server/src/__tests__/quota-windows-service.test.ts (added, +56/-0)
  • server/src/__tests__/quota-windows.test.ts (added, +812/-0)
  • server/src/adapters/registry.ts (modified, +4/-0)
  • server/src/index.ts (modified, +7/-10)
  • server/src/routes/agents.ts (modified, +15/-0)
  • server/src/routes/companies.ts (modified, +20/-1)
  • server/src/routes/costs.ts (modified, +207/-5)
  • server/src/routes/issues.ts (modified, +13/-0)
  • server/src/services/agents.ts (modified, +67/-7)
  • server/src/services/approvals.ts (modified, +16/-0)
  • server/src/services/budgets.ts (added, +958/-0)
  • server/src/services/companies.ts (modified, +67/-10)
  • server/src/services/costs.ts (modified, +221/-61)
  • server/src/services/dashboard.ts (modified, +9/-0)
  • server/src/services/finance.ts (added, +134/-0)
  • server/src/services/heartbeat.ts (modified, +342/-84)
  • server/src/services/index.ts (modified, +2/-0)
  • server/src/services/quota-windows.ts (added, +64/-0)
  • ui/src/App.tsx (modified, +1/-0)
  • ui/src/api/budgets.ts (added, +20/-0)
  • ui/src/api/costs.ts (modified, +41/-9)
  • ui/src/components/AccountingModelCard.tsx (added, +69/-0)
  • ui/src/components/ApprovalCard.tsx (modified, +4/-1)
  • ui/src/components/ApprovalPayload.tsx (modified, +25/-1)
  • ui/src/components/BillerSpendCard.tsx (added, +145/-0)
  • ui/src/components/BudgetIncidentCard.tsx (added, +100/-0)
  • ui/src/components/BudgetPolicyCard.tsx (added, +219/-0)
  • ui/src/components/BudgetSidebarMarker.tsx (added, +13/-0)
  • ui/src/components/ClaudeSubscriptionPanel.tsx (added, +140/-0)
  • ui/src/components/CodexSubscriptionPanel.tsx (added, +157/-0)
  • ui/src/components/FinanceBillerCard.tsx (added, +44/-0)
  • ui/src/components/FinanceKindCard.tsx (added, +43/-0)
  • ui/src/components/FinanceTimelineCard.tsx (added, +71/-0)
  • ui/src/components/ProviderQuotaCard.tsx (added, +416/-0)
  • ui/src/components/QuotaBar.tsx (added, +65/-0)
  • ui/src/components/SidebarAgents.tsx (modified, +16/-8)
  • ui/src/components/SidebarProjects.tsx (modified, +2/-0)
  • ui/src/context/LiveUpdatesProvider.tsx (modified, +4/-0)

Code Example

// agent-wait-dedupe.ts L3-7
export type AgentWaitTerminalSnapshot = {
  status: "ok" | "error" | "timeout";
  startedAt?: number;
  endedAt?: number;
  error?: string;
};

---

// agent.ts L790-796
respond(true, {
  runId,
  status: snapshot.status,
  startedAt: snapshot.startedAt,
  endedAt: snapshot.endedAt,
  error: snapshot.error,
});

---

export type AgentWaitTerminalSnapshot = {
  status: "ok" | "error" | "timeout";
  startedAt?: number;
  endedAt?: number;
  error?: string;
  meta?: {
    usage?: {
      input: number;
      output: number;
      cacheRead?: number;
      cacheWrite?: number;
    };
    lastCallUsage?: {
      input: number;
      output: number;
    };
    costUsd?: number;
    provider?: string;
    model?: string;
  };
};
RAW_BUFFERClick to expand / collapse

Summary

The agent.wait WebSocket response currently returns only { status, startedAt, endedAt, error }. External orchestrators like Paperclip need per-run token usage and cost data to enforce budgets and track spend — but this data never reaches the wire despite being computed internally.

The Gap

OpenClaw already tracks this internally:

  • normalizeUsage() in src/agents/usage.ts handles every provider format
  • estimateUsageCost() in src/utils/usage-format.ts computes per-model costs
  • loadSessionCostSummary() in src/infra/session-cost-usage.ts aggregates per-session costs from JSONL transcripts
  • Gateway methods usage.cost and sessions.usage expose this data to OpenClaw's own UI

But agent.wait doesn't include any of it:

// agent-wait-dedupe.ts L3-7
export type AgentWaitTerminalSnapshot = {
  status: "ok" | "error" | "timeout";
  startedAt?: number;
  endedAt?: number;
  error?: string;
};
// agent.ts L790-796
respond(true, {
  runId,
  status: snapshot.status,
  startedAt: snapshot.startedAt,
  endedAt: snapshot.endedAt,
  error: snapshot.error,
});

Proposed Change

Extend AgentWaitTerminalSnapshot to include an optional meta field:

export type AgentWaitTerminalSnapshot = {
  status: "ok" | "error" | "timeout";
  startedAt?: number;
  endedAt?: number;
  error?: string;
  meta?: {
    usage?: {
      input: number;
      output: number;
      cacheRead?: number;
      cacheWrite?: number;
    };
    lastCallUsage?: {
      input: number;
      output: number;
    };
    costUsd?: number;
    provider?: string;
    model?: string;
  };
};

At the end of an agent run, accumulate session usage from the JSONL transcript (the infrastructure for this already exists in session-cost-usage.ts) and attach it to the terminal snapshot before sending the agent.wait response.

Why This Matters

  • Paperclip's OpenClaw Gateway adapter (@paperclipai/adapter-openclaw-gateway) already has parseUsage() ready to consume usage, costUsd, provider, and model from response metadata — it just gets undefined today.
  • Paperclip PR #884 fixes the meta extraction priority chain on the consuming side.
  • Paperclip PR #949 (merged) added full billing ledger support including biller vs provider distinction, budget enforcement, and cost UI — all waiting for upstream cost data.
  • Without this, OpenClaw agents in Paperclip show $0 spend and budget enforcement never triggers.

Scope

~30-40 lines across:

  1. agent-wait-dedupe.ts — type extension
  2. Agent run lifecycle — accumulate usage from session transcript at run completion
  3. agent.ts — include meta in the agent.wait response

Backward compatible — meta is optional, existing consumers ignore unknown fields.

Related

  • Paperclip PR #884 — consuming side meta extraction fix
  • Paperclip PR #949 — billing ledger and cost control plane
  • Paperclip PR #386 — heartbeat cost recording fix (merged in v0.3.1)

extent analysis

Fix Plan

To fix the issue, we need to extend the AgentWaitTerminalSnapshot type and accumulate session usage from the JSONL transcript at the end of an agent run. Here are the concrete steps:

  • Extend the AgentWaitTerminalSnapshot type in agent-wait-dedupe.ts to include an optional meta field:
export type AgentWaitTerminalSnapshot = {
  status: "ok" | "error" | "timeout";
  startedAt?: number;
  endedAt?: number;
  error?: string;
  meta?: {
    usage?: {
      input: number;
      output: number;
      cacheRead?: number;
      cacheWrite?: number;
    };
    lastCallUsage?: {
      input: number;
      output: number;
    };
    costUsd?: number;
    provider?: string;
    model?: string;
  };
};
  • Accumulate session usage from the JSONL transcript at the end of an agent run using the existing infrastructure in session-cost-usage.ts. For example:
const sessionUsage = await loadSessionCostSummary(runId);
const meta = {
  usage: sessionUsage.usage,
  costUsd: sessionUsage.costUsd,
  provider: sessionUsage.provider,
  model: sessionUsage.model,
};
  • Include the meta field in the agent.wait response in agent.ts:
respond(true, {
  runId,
  status: snapshot.status,
  startedAt: snapshot.startedAt,
  endedAt: snapshot.endedAt,
  error: snapshot.error,
  meta,
});

Verification

To verify that the fix worked, you can check the agent.wait response for the presence of the meta field and its contents. You can also test the consuming side, such as the Paperclip adapter, to ensure that it can correctly extract and use the usage and cost data from the response metadata.

Extra Tips

  • Make sure to handle cases where the meta field is not present or is empty, to ensure backward compatibility with existing consumers.
  • Consider adding logging or monitoring to track the usage and cost data being sent in the agent.wait response, to help with debugging and troubleshooting.

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