← Back to Pull requests

perf(frontend): serialize Anthropic stream deltas without a JSON value tree #18813

Open
unknown opened this pull request with 441 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 15, 2026 16:11 UTC
Author

Overview

On /v1/messages streaming, each content_block_delta and ping event carries a usage object (#12473). To add it, event_json_with_usage builds 2 serde_json::Value trees, inserts 2 keys, and writes the tree to a string. This occurs for each output token.

This PR writes the same JSON directly from a borrowed wrapper struct. The bytes on the wire do not change. The converter takes 66% less time for each text token.

Key Changes

Core logic: 29 lines added and 19 removed, in one file: stream_converter.rs.

  • event_json_with_usage returns the JSON string. For an event that carries usage, it serializes a wrapper struct with #[serde(flatten)]. Other events do not change.

  • total_tokens uses the same saturating sum as before.

  • The published usage logic from #18377 does not change.
  • Benchmark: the new Criterion bench anthropic_stream_deltas, with a [[bench]] entry in lib/llm/Cargo.toml and a section in lib/llm/benches/README.md.

    Why the bytes do not change: dynamo-llm always enables serde_json/preserve_order through its required dependencies. Thus, both paths write the keys in the same order. Both paths skip cache fields that are None.

    Validation

    Wire bytes. The new test test_usage_triple_bytes_match_value_insertion compares the new string with the old path, byte for byte. It covers text, thinking, signature, input-JSON, ping and content_block_stop events, each with 3 usage shapes. The existing snapshot anthropic_http_replay::streaming_text_baseline passes with no change. On an earlier base (cde22ae519), I moved total_tokens to a different position. Both tests failed.

    Cost. The bench anthropic_stream_deltas runs AnthropicStreamConverter::append_chunk_events over 256 text chunks. Each delta carries the usage triple. The bench uses only the public API, so the same file also runs on main. I ran it on main (9f1f0829de) with --save-baseline main, then on this PR with --baseline main, on the same machine.

    | 256 text chunks | main | This PR | Change |
    | --- | --- | --- | --- |
    | Linux x86_64 (2 vCPU Xeon 2.6 GHz, rustc 1.96.1) | 321.4 µs | 109.5 µs | -66.5% |
    | macOS arm64 (Apple Silicon) | 314.5 µs | 101.2 µs | -67.8% |

    The Linux machine has 2 GB of RAM. Thus, for both Linux builds I set CARGO_PROFILE_BENCH_CODEGEN_UNITS=16, CARGO_PROFILE_BENCH_LTO=false and CARGO_PROFILE_BENCH_DEBUG=0. The macOS builds use the repository bench profile.

    This saves about 0.83 µs of frontend CPU for each output token. I did not do a mocker A/B test. The saving is smaller than the noise of an end-to-end run. The benchmark measures the changed step directly.

    I ran the tests on macOS arm64 with -p dynamo-llm --no-default-features, after I merged main (9f1f0829de). The 56 stream_converter tests pass. I did not run the Linux CI matrix or the default features.

    Issues and Ongoing Work

  • Closes #18816

  • I found no duplicate PR or issue (search on 2026-11-15).

  • #18377 (merged) added ping to the events that carry usage. This PR keeps that rule.

  • #17381 (open) changes other parts of this file. It already conflicts with main.
  • <details>
    <summary>Pull Request Checklist</summary>

  • I used Dynamo's /pr-checklist skill.
  • </details>

    Diff: 441 lines (+330 / -111) across 5 files Reviewer: luckyq Review duration: 28395m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 441 lines diff across 5 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.364
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #18813"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.3% (MEDIUM) · Higher = larger blast radius
    50.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.8% · Safe to merge
    26.8%