Skip to content

feat(mcp): token-count telemetry on code_*/scout/invoke#75

Closed
jmagar wants to merge 1 commit into
code-mode-v2-drop-lab-actionsfrom
feat/gateway-token-telemetry
Closed

feat(mcp): token-count telemetry on code_*/scout/invoke#75
jmagar wants to merge 1 commit into
code-mode-v2-drop-lab-actionsfrom
feat/gateway-token-telemetry

Conversation

@jmagar
Copy link
Copy Markdown
Owner

@jmagar jmagar commented May 25, 2026

Summary

  • Surface input_tokens and output_tokens on every dispatch event for the five gateway meta-tools — code_search, code_schema, code_execute, scout, and invoke. The existing elapsed_ms field already gave us timing; tokens close the gap so log analytics can spot ballooning payloads and size LLM context budgets.
  • Helpers: `estimate_tokens(&str)`, `estimate_tokens_value(&Value)`, `estimate_tokens_args(&Map)` using the chars/4 heuristic (dependency-free; pull in tiktoken-rs later if exact counts are needed).
  • Input tokens computed once at handler entry from the MCP arguments map. Output tokens computed before each success log against the serialized result. Failure paths only emit `input_tokens` (no useful output to size).

Test plan

  • `cargo check --manifest-path crates/lab/Cargo.toml --all-features` clean
  • `cargo test --lib estimate_tokens` — 3 new tests pass
  • `cargo test --lib gateway` — 210 passed, 23 ignored (no regressions)
  • `cargo test --test code_mode_runner` — 2 passed
  • Manual verification: hit a `scout` / `invoke` call via Labby and grep stderr for `input_tokens`/`output_tokens` fields

Summary by cubic

Adds token-count telemetry to gateway meta-tools and ships Code Mode v2 with upstream-only execution, JS catalog search, and capped responses. Improves observability and tightens safety.

  • New Features

    • Telemetry: logs input_tokens/output_tokens for code_search, code_execute, scout, and invoke alongside elapsed_ms. Inputs computed once from MCP args; outputs on success; failures log inputs only. Adds estimate_tokens* helpers (chars/4) with 3 tests.
    • Code Mode v2: drops Lab-action support (only upstream::<server>::<tool> IDs). Adds response budgets (byte/token caps) and maps code_mode_timeout/code_mode_fuel_exhausted to HTTP 504. code_search now filters an inlined catalog with JavaScript; code_schema is removed. Optional code_mode_wasm feature using javy and wasmtime with a helper script to fetch the Javy plugin.
  • Migration

    • Replace any Code Mode calls to lab::<service>.<action> with tool_execute/invoke; Code Mode accepts upstream tool IDs only.
    • Stop using code_schema; use scout for discovery or code_search for JS catalog filtering.
    • Respect new Code Mode response caps via [code_mode] config.
    • MSRV bumped to Rust 1.92; Docker image now installs libclang for Code Mode builds.

Written for commit f8c20ca. Summary will update on new commits. Review in cubic

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 25, 2026

Warning

Review limit reached

@jmagar, we couldn't start this review because you've used your available PR reviews for now.

Your plan includes 1 review of capacity. Refill in 16 minutes and 45 seconds.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more review capacity refills, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 56aa1bf0-c169-4a58-ae10-c5412cd11be8

📥 Commits

Reviewing files that changed from the base of the PR and between d25a8af and f8c20ca.

⛔ Files ignored due to path filters (4)
  • Cargo.lock is excluded by !**/*.lock and included by **/*
  • docs/generated/cli-help.md is excluded by !**/generated/** and included by **/*
  • docs/generated/feature-matrix.json is excluded by !**/generated/** and included by **/*
  • docs/generated/feature-matrix.md is excluded by !**/generated/** and included by **/*
📒 Files selected for processing (18)
  • Cargo.toml
  • config/Dockerfile
  • crates/lab/Cargo.toml
  • crates/lab/src/api/error.rs
  • crates/lab/src/cli/gateway.rs
  • crates/lab/src/config.rs
  • crates/lab/src/dispatch/gateway/code_execute_description.md
  • crates/lab/src/dispatch/gateway/code_mode.rs
  • crates/lab/src/main.rs
  • crates/lab/src/mcp/CLAUDE.md
  • crates/lab/src/mcp/catalog.rs
  • crates/lab/src/mcp/server.rs
  • crates/lab/tests/code_mode_runner.rs
  • deny.toml
  • docs/dev/CODE_MODE.md
  • docs/dev/ERRORS.md
  • docs/superpowers/plans/2026-05-25-code-mode-v2-drop-lab-actions.md
  • scripts/refresh-javy-plugin.sh
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/gateway-token-telemetry

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9d4479bdc5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/// and accurate enough for capacity tracking; do NOT use for LLM budget
/// enforcement — pull in `tiktoken-rs` if exact counts are required.
fn estimate_tokens(s: &str) -> usize {
s.len().div_ceil(4)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Count Unicode chars instead of UTF-8 bytes

estimate_tokens documents a char-based heuristic (~4 chars/token) but uses s.len(), which counts UTF-8 bytes in Rust. For non-ASCII inputs (for example CJK text or emoji in code_execute/scout payloads), this inflates input_tokens/output_tokens and makes the new telemetry systematically inaccurate, which can mislead capacity/cost monitoring and trigger false regressions. Use a character count (or explicitly rename/document this as byte-based) to keep the metric semantics consistent.

Useful? React with 👍 / 👎.

Surface input_tokens and output_tokens fields on every dispatch event for
the five gateway meta-tools — code_search, code_schema, code_execute,
scout, and invoke. The fields complement the existing elapsed_ms and
make it possible to size LLM context budgets and spot ballooning
responses from log analytics.

Uses a simple chars/4 estimator (chars div_ceil 4) — dependency-free and
accurate enough for capacity tracking. Pull in tiktoken-rs later if
exact counts are required.

Input tokens are computed once at handler entry from the MCP arguments
map. Output tokens are computed before the success log emits, against
the serialized result. Failure paths only emit input_tokens (no useful
output to size).

Refs: timing already existed inline; this commit adds token accounting
on the same boundary. New estimator helpers covered by 3 unit tests.
@jmagar jmagar force-pushed the feat/gateway-token-telemetry branch from 9d4479b to f8c20ca Compare May 25, 2026 15:02
@jmagar jmagar changed the base branch from main to code-mode-v2-drop-lab-actions May 25, 2026 15:02
@jmagar jmagar deleted the branch code-mode-v2-drop-lab-actions May 25, 2026 18:05
@jmagar jmagar closed this May 25, 2026
@jmagar jmagar deleted the feat/gateway-token-telemetry branch May 25, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant