Skip to content

feat(obs): add model label to aisix_llm_tokens_by_client_total and record it on /v1/responses - #784

Merged
jarvis9443 merged 3 commits into
mainfrom
feat/1044-tokens-by-client-model
Jul 20, 2026
Merged

feat(obs): add model label to aisix_llm_tokens_by_client_total and record it on /v1/responses#784
jarvis9443 merged 3 commits into
mainfrom
feat/1044-tokens-by-client-model

Conversation

@jarvis9443

@jarvis9443 jarvis9443 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Fixes api7/AISIX-Cloud#1044
Fixes api7/AISIX-Cloud#1091

What

  1. model label on aisix_llm_tokens_by_client_total. Every series now carries the requested logical model — the exact value the same request records in the aisix_llm_* families' model label — so sum by (client_type, model) can answer "which models is each client type spending tokens on" (per-client model-usage analysis, cost governance, migration tracking).
  2. /v1/responses now records the by-client series. Codex is an allowlisted client_type but talks to /v1/responses, which previously recorded nothing into this metric — codex traffic was invisible. The recording sits in the endpoint's single usage funnel (emit_usage_event), covering the non-streaming, verbatim-streaming, and cross-provider bridge-streaming paths.

Design notes

Compatibility

Adding a label starts new time series; the old label-set series stop growing. Aggregations like sum by (client_type) keep working; only exact-matcher queries pinning the full label set need updating. Docs PR updating the metrics reference: api7/docs (paired).

Tests

  • Unit: model label on every token_type slice; one client × two models → six independent series with per-model counts; all-zero no-op unchanged.
  • E2E (real aisix + etcd + mock upstream): one client_type calling two chat models (non-streaming + streaming) yields two per-model series with correct counts; /v1/responses with a codex UA produces the series (fails before the fix — the endpoint recorded nothing); the #1002 cache-inclusive messages test now also pins the model label.

Summary by CodeRabbit

  • New Features

    • Token usage metrics now include the logical model, allowing usage to be viewed separately by client type and model.
    • Usage metrics now cover Responses API requests, including cache-inclusive token totals.
  • Bug Fixes

    • Standardized model labeling across streaming, non-streaming, and Responses API requests.
  • Tests

    • Added end-to-end coverage for per-model token metrics across supported request types.

…n /v1/responses

aisix_llm_tokens_by_client_total now carries a model label (the requested
logical model, same value as the aisix_llm_* families' model) so operators
can see which models each client type spends tokens on. The /v1/responses
usage funnel now records the series too — codex is an allowlisted
client_type but talks to /v1/responses, which previously recorded nothing,
leaving codex traffic invisible in the metric.

Fixes api7/AISIX-Cloud#1044
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 47 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

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

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e3b2bacb-1f25-4d43-8c7f-c6b0ff475b90

📥 Commits

Reviewing files that changed from the base of the PR and between f8dddcb and 32a3f20.

📒 Files selected for processing (1)
  • tests/e2e/src/cases/tokens-by-client-model-1044-e2e.test.ts
📝 Walkthrough

Walkthrough

aisix_llm_tokens_by_client_total now includes the logical model label. Chat, Anthropic, and Responses telemetry paths pass that model value, while unit and E2E tests validate separate per-model series and token counts.

Changes

Client token metrics by model

Layer / File(s) Summary
Metric contract and recording
crates/aisix-obs/src/metrics.rs
The public recording method accepts a logical model and emits input, output, and total series labeled by client_type and model; unit tests cover independent model series.
Proxy telemetry wiring
crates/aisix-proxy/src/chat.rs, crates/aisix-proxy/src/messages.rs, crates/aisix-proxy/src/responses.rs
Chat, Anthropic, and Responses usage paths pass the logical model when recording token metrics, including cache-inclusive Responses totals.
Metric end-to-end validation
tests/e2e/src/cases/tokens-by-client-model-1044-e2e.test.ts, tests/e2e/src/cases/tokens-by-client-total-1002-e2e.test.ts
E2E coverage validates model-labeled series for multiple chat models and the Responses endpoint; existing metric filtering receives a clarifying comment.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ChatEndpoint
  participant MessagesEndpoint
  participant ResponsesEndpoint
  participant Metrics
  Client->>ChatEndpoint: Send chat completion request
  ChatEndpoint->>Metrics: Record tokens with client type and logical model
  Client->>MessagesEndpoint: Send Anthropic messages request
  MessagesEndpoint->>Metrics: Record usage with client type and logical model
  Client->>ResponsesEndpoint: Send Responses request
  ResponsesEndpoint->>Metrics: Record usage and cache-inclusive total
  Metrics-->>Client: Expose input, output, and total metric series
Loading

Suggested reviewers: moonming

🚥 Pre-merge checks | ✅ 4 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR includes an unrelated pnpm-workspace.yaml cleanup, which is outside the metric-labeling scope. Move the pnpm-workspace.yaml cleanup to a separate PR so this change set stays focused on #1044.
E2e Test Quality Review ❓ Inconclusive temporary Need inspection of the new E2E file and helpers to verify coverage and any hidden test-order dependency.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a model label to by-client LLM token metrics and recording it for /v1/responses.
Linked Issues check ✅ Passed The PR satisfies #1044 by adding the model label to by-client token series, using the logical model, updating chat/messages/responses paths, and adding unit/E2E tests.
Security Check ✅ Passed Changes only add bounded metric labels/tests; call sites use resolved logical models, with no new logs, auth, DB, TLS, or secret-handling paths.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/1044-tokens-by-client-model

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/e2e/src/cases/tokens-by-client-model-1044-e2e.test.ts`:
- Around line 144-148: Update the shared beforeAll setup containing createApiKey
to await waitConfigPropagation after seeding the models, then remove the
test-local wait so every test, including the Responses test, relies on shared
configuration readiness rather than execution order.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 106e1eb4-771f-404b-a5bf-780aa8486096

📥 Commits

Reviewing files that changed from the base of the PR and between 90df23f and f8dddcb.

📒 Files selected for processing (6)
  • crates/aisix-obs/src/metrics.rs
  • crates/aisix-proxy/src/chat.rs
  • crates/aisix-proxy/src/messages.rs
  • crates/aisix-proxy/src/responses.rs
  • tests/e2e/src/cases/tokens-by-client-model-1044-e2e.test.ts
  • tests/e2e/src/cases/tokens-by-client-total-1002-e2e.test.ts

Comment thread tests/e2e/src/cases/tokens-by-client-model-1044-e2e.test.ts
Both tests now rely on shared readiness instead of the Responses test
inheriting the first test's wait, so each is independently runnable.
@jarvis9443
jarvis9443 merged commit 5f6f7fa into main Jul 20, 2026
12 checks passed
@jarvis9443
jarvis9443 deleted the feat/1044-tokens-by-client-model branch July 20, 2026 07:19
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