NeMo-Relay icon indicating copy to clipboard operation
NeMo-Relay copied to clipboard

[Bug]: Anthropic streaming overwrites `message_start` usage with `null` values from `message_delta`

Open Sakshitapkir03 opened this issue 2 weeks ago • 2 comments

Affected area

Rust core runtime

Current behavior

AnthropicMessagesStreamingCodec merges message_delta usage into the message_start usage with current.extend(update.clone()) (observe_usage in crates/codecs/src/anthropic.rs). Since #996, fields that message_delta omits survive, but a field present as null overwrites the earlier value.

Anthropic's schema allows these nulls: the Ruby SDK defines the MessageDeltaUsage input and cache counters as required ..., Integer, nil?: true, and Anthropic's Python SDK accumulator (anthropic 0.120.2, lib/streaming/_messages.py) copies input_tokens, cache_creation_input_tokens, cache_read_input_tokens, and server_tool_use from message_delta only when they are not None. anomalyco/opencode#44116 addresses the same case, keeping the earlier message_start count when the terminal value is null.

The decoder reads null as absent, so such a stream loses the input and cache token counts reported in message_start, which telemetry and cost estimation use. The same code is on release/0.11 (crates/core/src/codec/anthropic.rs).

Expected behavior

A null field in message_delta usage should leave the earlier value in place, while non-null values, including 0, replace it, matching Anthropic's SDK accumulator.

Steps to reproduce

  1. Add to crates/codecs/tests/unit/anthropic_tests.rs:
    #[test]
    fn anthropic_streaming_codec_keeps_message_start_usage_when_delta_reports_null() {
        let codec = AnthropicMessagesStreamingCodec::new();
        let mut collector = codec.collector();
        collector(json!({
            "type": "message_start",
            "message": {
                "id": "msg_1", "type": "message", "role": "assistant",
                "model": "claude-sonnet-4-20250514", "content": [],
                "stop_reason": null, "stop_sequence": null,
                "usage": {
                    "input_tokens": 10,
                    "cache_creation_input_tokens": 200,
                    "cache_read_input_tokens": 900,
                    "output_tokens": 1
                }
            }
        }))
        .unwrap();
        collector(json!({
            "type": "message_delta",
            "delta": {"stop_reason": "end_turn", "stop_sequence": null},
            "usage": {
                "input_tokens": null,
                "cache_creation_input_tokens": null,
                "cache_read_input_tokens": null,
                "output_tokens": 15
            }
        }))
        .unwrap();
    
        let usage = &codec.finalizer()()["usage"];
        assert_eq!(usage["input_tokens"], json!(10));
        assert_eq!(usage["cache_creation_input_tokens"], json!(200));
        assert_eq!(usage["cache_read_input_tokens"], json!(900));
        assert_eq!(usage["output_tokens"], json!(15));
    }
    
  2. Run cargo nextest run -p nemo-relay-codecs -E 'test(delta_reports_null)'.
  3. It fails at the first assertion: left: Null, right: Number(10).

Environment

  • NeMo Relay version or commit: main at 2acd6ae1
  • OS and architecture: macOS 27.2.1 , Apple Silicon (arm64); platform-independent
  • Rust version: 1.96.1 (pinned by rust-toolchain.toml)
  • Python version: 3.12.15 (only to inspect the anthropic SDK)
  • Node.js version: N/A
  • Go version: N/A
  • Install/build command: from source; cargo-nextest 0.9.133

Impact

Streamed Anthropic calls whose message_delta reports usage fields as null, as Anthropic's schema allows, lose their input and cache token counts. Non-streamed calls are unaffected.

Proposed fix: in observe_usage, skip null values when merging (update.iter().filter(|(_, value)| !value.is_null())). With it, the test above and the rest of the codec suite pass (564 tests); cargo fmt and Clippy are clean. I'll target whichever branch you prefer.

I'd like to work on this and will open a PR once you're OK with the approach.

Sakshitapkir03 avatar Nov 19 '26 17:11 Sakshitapkir03

Great stuff, thanks! Please target release/0.11

greysonlalonde avatar Nov 19 '26 18:11 greysonlalonde

@greysonlalonde opened #1778 against release/0.11.

Sakshitapkir03 avatar Nov 20 '26 00:11 Sakshitapkir03