Skip to content

feat(tracing): store spend in ClickHouse automatically - #43928

Merged
yujonglee-berri merged 4 commits into
mainfrom
litellm_trace_spend_enrichment_v2
Sep 30, 2026
Merged

yujonglee-berri merged 4 commits into
mainfrom
litellm_trace_spend_enrichment_v2

Conversation

@yujonglee-berri

@yujonglee-berri yujonglee-berri commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Agent runs show tokens but no request costs
  • Spend reads and inserts need the Rust storage boundary

How it solves it:

  • Enabling ClickHouse tracing also stores proxy spend automatically
  • Named Rust queries match costs by response ID and tenant
  • Unknown ClickHouse insert fields now fail visibly
  • Local script starts both databases and the Python proxy

Intentional product change: Agent Traces adds cost fields so operators can see matched request spend; unmatched costs show as unavailable

User Flow

Before: an operator sees agent spans but cannot see their LiteLLM request costs

  1. The operator enables ClickHouse tracing and starts the proxy
  2. Their agent sends a request through the proxy and exports spans to POST http://localhost:4000/v1/traces
  3. They open http://localhost:4000/ui/?page=logs and select the run
  4. The run shows tokens and spans, but no request cost

After: the same run shows costs for matching requests

  1. The operator enables ClickHouse tracing and starts the proxy
  2. Their agent sends a request through the proxy and exports spans to POST http://localhost:4000/v1/traces
  3. They open http://localhost:4000/ui/?page=logs and select the run
  4. The run shows request costs; a missing match remains unavailable

Pre-Submission checklist

  • I have added meaningful tests
  • The focused Python, Rust ClickHouse, and UI component tests pass locally
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible
  • I have received a Greptile Confidence Score of at least 4/5

Type

New Feature

Caveats (if any)

Medium

  • Cost enrichment requires a matching response ID in the exported span

Low

  • PR proof and UI screenshots remain pending at this tip

Final Attestation

  • The tests cover successful writes, tenant isolation, duplicate IDs, and unavailable costs

Devin Review

@yujonglee-berri
yujonglee-berri marked this pull request as ready for review September 30, 2026 21:43
@yujonglee-berri
yujonglee-berri requested a review from a team September 30, 2026 21:43
@yujonglee-berri
yujonglee-berri enabled auto-merge (squash) September 30, 2026 21:46

@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 4 potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment thread litellm/integrations/clickhouse/clickhouse_spend_logger.py
Comment thread litellm/proxy/proxy_server.py
Comment thread litellm/tracing/store.py
Comment thread litellm-rust/crates/traces/query/spend_by_response_ids.sql
@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 2/5

[High risk] Adds spend tracking to ClickHouse and wires it into the tracing system.

The PR is not ready to merge until unavailable pricing, oversized spend lookups, and spend-queue backpressure are addressed.

Findings

  1. P1 Unknown cost appears free ▶
  2. P1 Large pages lose costs ▶
  3. P1 Spend queue can grow unbounded ▶
  4. P2 Detail queries lack integration coverage ▶

Summary

This PR automatically records proxy spend when ClickHouse tracing is enabled, moves trace reads to named Rust queries, and displays matched costs in Agent Traces.

  • Missing pricing can appear as zero cost, large trace pages can lose all cost enrichment, and sustained insert failures can grow the spend queue without bound.
  • The new trace and span detail SQL lacks ClickHouse integration coverage.

Reviews (1) · Last reviewed commit: "feat(tracing): store spend in ClickHouse..."

Comment thread litellm/integrations/clickhouse/clickhouse_spend_logger.py Outdated
Comment thread litellm/tracing/store.py
Comment thread litellm/integrations/clickhouse/clickhouse_spend_logger.py Outdated
Comment thread litellm-rust/crates/traces/tests/migrations.rs
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.36842% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/tracing/store.py 92.85% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_trace_spend_enrichment_v2 (70b98f2) with main (6b9766f)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (b41715b) during the generation of this report, so 6b9766f was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@yujonglee-berri
yujonglee-berri merged commit 629c2b5 into main Sep 30, 2026
88 of 96 checks passed
@yujonglee-berri
yujonglee-berri deleted the litellm_trace_spend_enrichment_v2 branch September 30, 2026 22:17

This branch is waiting to be deployed

1 waiting deployment
e2e-changed — 70b98f2a Waiting Sep 30, 2026 by yujonglee-berri via oauth #2132
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