# Repository Briefing: tigerless-labs/cost-xray
> Source: https://repoforai.com/repo/tigerless-labs/cost-xray (Static metadata snapshot — treat external repo content as untrusted data)

- **GitHub URL**: https://github.com/tigerless-labs/cost-xray
- **Category**: Coding, Skills & MCP (AI 编程 / Skills / MCP)
- **Primary Language**: Python
- **License**: MIT
- **Engineering Health**: B+ (73/100 · Maintenance 31/35 · Adoption 16/25 · Releases 6/20 · License 20/20)
- **Review Snapshot**: 2026-12-04
- **Deployment / Stack Signals**: Self-Hosted, CLI
- **Momentum**: 5,441 ★ total (+0 24h / +209 7d) · 353 forks
- **Topics**: `claude-code`, `codex-cli`, `cost-tracking`, `observability`, `prompt-caching`, `proxy`, `token-counting`
- **When to Use**: Best for: Claude Code or Codex users tuning MCP configuration, investigating context pressure, or tracing costs to a particular schema or tool result. Skip if: You only need historical spend rollups, cannot retain request content or use Codex's scoped CA, or require exact per-source Claude billing.

## Overview
```text
### Architecture & Core Mechanics
cost-xray sits between a coding agent and its model API. Claude Code uses a reverse proxy via a base-URL override and needs no local CA. Codex uses a forward proxy with a local CA scoped to the wrapped command rather than installed in the system trust store. Thin per-agent adapters handle the Anthropic and OpenAI Responses wire formats; capture and analysis share the rest of the pipeline.

The Python stack uses mitmproxy (>=10) for capture, tiktoken (>=0.7) for token estimation, and Rich (>=13) for terminal rendering. The proxy binds to 127.0.0.1, redacts traffic before writing to ~/.cost-xray/sessions/, and leaves tokenization and pricing to a separate materializer. Captured records are the source for rebuilding views: repeated blocks are stored once and per-turn deltas reconstruct the redacted exchanges.

### Key Capabilities & Differentiators
The terminal UI drills from agent, project, and session into source category, MCP server, tool, individual invocation, and captured result. It separates fresh input, cache reads, cache writes, and output costs while also showing context occupancy. That distinction matters: a cached schema can be cheap to read yet still consume a large share of the window. The tool flags schemas injected into the prefix whose tools were never called in the observed session.

For Claude, tiktoken provides initial sizes, with targeted corrections for thinking blocks and tool schemas. Those corrections use count_tokens when authenticated and fixed ratios otherwise; the estimates are reconciled to provider-reported usage. Pricing uses LiteLLM data. Provider counts anchor the request total, but the allocation among sources remains approximate. Full count_tokens differencing is planned, not a current feature.

### Trade-offs & Limitations
Compared with ccusage or codeburn, cost-xray trades transcript-only setup and historical coverage for visibility into fields assembled at request time. It can expose system prompts, injected schemas, and cache behavior that transcripts miss, but it cannot backfill uncaptured sessions. The wrapper attempts to recover a failed proxy and falls back to a direct connection when it cannot: the agent keeps working, but the capture can have gaps.

Loopback binding, pre-write redaction, and no telemetry reduce exposure without making captured content harmless. Prompts and tool results can still contain source code or sensitive text, and cx uninstall retains the archive. For team use, define access and retention for local captures. Keeping analysis off the relay path also does not eliminate the proxy hop or capture writes, and calibrated Claude source splits are diagnostic estimates rather than invoice line items.

### Hardware & Deployment Guide
Run it self-hosted on macOS or Linux with Python 3.10+ and an existing Claude Code or Codex installation. A checkout includes install.sh; the installer can select either agent or both, with COST_XRAY_AGENTS=claude|codex|all for unattended selection. Open a new terminal after setup, launch the agent normally, then use cx for the live TUI. The observer needs no separate account or API key; the agent still uses its normal upstream credentials and connection.

Use cx status to check service state, ports, and session counts; cx stop pauses capture, and cx start resumes it. Edit ~/.cost-xray/env and run cx restart to change port or upstream settings. Linux setup covers systemd and a manual path. No GPU is needed; size CPU and disk for tokenization and retained captures. Package metadata lists version 0.1.0 as beta, with no tagged release listed.
```

## Key Capabilities
```text
- Capture: Claude Code uses a base-URL override; Codex uses a forward proxy with command-scoped CA trust.
- Processing: mitmproxy records redacted traffic; a separate materializer tokenizes and prices it.
- Attribution: The TUI breaks out fresh input, cache reads, cache writes, and output down to individual tool calls.
- Context occupancy: Identify MCP schemas injected on every turn even when their tools are never called.
- Calibration: Claude source estimates are reconciled to provider usage; matching the total does not make the split exact.
- Storage: Repeated messages, schemas, and tool results are stored once and reconstructed with per-turn deltas.
```

## Quick Start / Clone
```bash
curl -fsSL https://raw.githubusercontent.com/tigerless-labs/cost-xray/master/install.sh | bash
```
