Skip to content

feat(spend-logs): rust spend log row builder - #52161

Merged
narmstrong-berri merged 1 commit into
nate_spend_logs_v2_countersfrom
nate_spend_logs_v2_row_builder
Nov 21, 2026
Merged

narmstrong-berri merged 1 commit into
nate_spend_logs_v2_countersfrom
nate_spend_logs_v2_row_builder

Conversation

@narmstrong-berri

@narmstrong-berri narmstrong-berri commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor
  • PR 2 of the spend logs on CustomLoggerV2 in Rust stack: every ledger-on SpendLogs row and its accounting_payload is built in Rust
  • New crate litellm-callbacks-spend-log (no pyo3), exported through python-bridge as RustSpendLogRowBuilder; Python glue litellm/proxy/spend_tracking/rust_spend_log_rows.py
  • Input parsed into typed structs at the boundary (input v2); free-form documents (metadata, request body, response, usage, guardrail records) stay JSON
  • Ported to Rust: message redaction, compaction-block handling, base request id and cost-row suffixes, cache-hit fields, end user, caller session, oauth / k8s / spillover stamps, classifier audit fields, ledger row finalize
  • Still Python, each needs Python-only state: usage extraction from live response objects, the in-place metadata merge, cache_key, served_from_cache, unknown-model message, router lookups, savings pricing, accounting windows
  • No fallback to the old Python builder; non-plain values are converted in the glue (weird values such as lone surrogates are not preserved)
  • New python-compat helpers: dict, pystr, truthy or, lax float, float sum, compact json dumps
  • Goldens recorded from main's Python first: 323 rows (incl. 8 redaction and compaction scenarios), 24 accounting payloads, 12 ledger rows; Rust matches every one byte for byte
  • Ledger-off path unchanged
  • Tests: spend_tracking, db, accounting and track_cost_callback suites 4086 passed on main and branch (same 7 environmental failures); lint and type counts flat against base
  • Note: the scenario test is named to run before test_configurable_string_length_env_var, which reloads litellm.constants and leaks a truncation default into later tests (existing leak)

Devin Review

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 45 untouched benchmarks


Comparing nate_spend_logs_v2_row_builder (80009ad) with nate_spend_logs_v2_counters (a712582)1

Open in CodSpeed

Footnotes

  1. No successful run was found on nate_spend_logs_v2_counters (683026b) during the generation of this report, so 4eef444 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from 613305f to 466360f Compare November 18, 2026 04:47
@narmstrong-berri
narmstrong-berri changed the base branch from main to nate_spend_logs_v2_counters November 18, 2026 05:25
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from 466360f to 80009ad Compare November 18, 2026 06:55
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from 80009ad to f3c5bb9 Compare November 19, 2026 19:31
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch 2 times, most recently from 6a785b8 to 8fe1e8b Compare November 19, 2026 22:40
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch 3 times, most recently from 285377a to 6b98f66 Compare November 20, 2026 01:31
@narmstrong-berri narmstrong-berri self-assigned this Nov 20, 2026
@narmstrong-berri
narmstrong-berri marked this pull request as ready for review November 20, 2026 02:31
@narmstrong-berri
narmstrong-berri requested a review from a team November 20, 2026 02:31
@veria-ai

veria-ai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

The PR introduces a Rust-backed spend log row builder, including preparation of model and error information for spend logging.

One issue has been addressed, and one remains open in error scrubbing. Removing NUL characters from model names before computing redaction variants can leave sensitive model text in logged error information for certain failed requests, even when prompt storage is disabled. The remaining concern is a narrow logging privacy regression rather than a broader access-control issue.

Open issues (1)

Fixed/addressed: 1 · PR risk: 4/10

Reviews

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

4 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +418 to +419
litellm_params: LitellmParams::parse(get_or_none(value, "litellm_params")),
kwargs: Kwargs::parse(get_or_none(value, "kwargs")),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Python coercion outside the required boundary

For ledger-on requests, RowInput::from_value coerces Python kwargs inside the callback crate. Workspace rules restrict these coercions to python-bridge and config.

Learn more

The Rust workspace's Python boundary skill restricts coercion of Python kwargs to python-bridge and configuration input to config. The callback crate currently receives JSON and interprets Python scalar types through Kwargs::parse, text_of, number_of, and RowAuthFacts::parse. RustSpendLogRowBuilder::build_row invokes that parsing inside the callback implementation rather than constructing its typed input at the bridge.

Example: A request's response_cost enters the callback crate as a generic value. Kwargs::parse decides whether it counts as a number there, rather than receiving an already resolved optional cost.

Recommended fix: Construct the typed row input in python-bridge and pass it to the callback crate. Keep opaque request, response and metadata documents as data where necessary, but resolve scalar kwargs and flags at the designated boundary.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +254 to +257
#[derive(Clone, Debug)]
pub(crate) struct RowAuthFacts {
/// `metadata.get("user_api_key")`, `None` when absent: its type decides how it is redacted.
pub(crate) user_api_key: Option<Value>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟨 Raw API keys in debug output

Formatting RowAuthFacts with Debug exposes its raw API key. The Rust redaction policy requires secret fields to hide their contents.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch 2 times, most recently from 0d85f52 to de4838a Compare November 20, 2026 04:51

@yujonglee-berri yujonglee-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks good

@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from de4838a to 40461d6 Compare November 20, 2026 18:45
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from 40461d6 to cfd2f70 Compare November 20, 2026 19:16
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from cfd2f70 to 0992d64 Compare November 20, 2026 19:48
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from 0992d64 to a6eedb9 Compare November 20, 2026 20:20
**_request_metadata(litellm_params),
},
"kwargs": {
**_projected(kwargs, _KWARGS_KEYS | _STORED_PROMPT_KWARGS_KEYS if stores_prompts else _KWARGS_KEYS),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low: Preserve raw model spellings until error scrubbing

_projected() applies safe_json_structure() to model, removing NUL characters before Rust computes its redaction spellings: a model containing medical\u0000 records no longer matches an error echo containing the escaped \x00 or \u0000 form, although the Python builder scrubbed both. For failed requests whose model is replaced with the placeholder, this leaves sensitive model text in error_information even with prompt storage disabled; pass the original model or precomputed spellings to the scrubber and remove NULs afterward.

@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from a6eedb9 to f39de83 Compare November 20, 2026 20:47
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from f39de83 to 031416b Compare November 20, 2026 21:11
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from 031416b to 557ecfd Compare November 20, 2026 21:21
@narmstrong-berri
narmstrong-berri force-pushed the nate_spend_logs_v2_row_builder branch from 557ecfd to d6b5c96 Compare November 21, 2026 00:18
@narmstrong-berri
narmstrong-berri merged commit 9220b94 into main Nov 21, 2026
69 of 118 checks passed
@narmstrong-berri
narmstrong-berri deleted the nate_spend_logs_v2_row_builder branch November 21, 2026 00:31
mateo-berri added a commit to zoveki/litellm that referenced this pull request Nov 23, 2026
Main now builds spend rows in Rust (BerriAI#52161), so the sub-call session fallback under missing_session_id: omit moves into the Rust row builder too, with a golden scenario for it
mateo-berri added a commit that referenced this pull request Nov 24, 2026
…_spend_name

main moved the spend log row body into legacy_spend_log_rows.py and made the Rust row builder the default (#52161, #52182), so the rule that keeps a registered agent's name on a failed /a2a row is carried into the legacy builder, the Rust builder and its row input, and the golden recorder
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.

2 participants