dexcost
Rust SDK for dexcost -- Agent Unit Economics platform. Track LLM costs, non-LLM service fees, and retry waste attributed to customers, projects, and workflows.
Installation
Or add to your Cargo.toml:
[]
= "0.1"
Quickstart
use ;
use dec;
async
Cloud Mode
To push events to the dexcost Control Layer:
use ;
init.unwrap;
Events are buffered in memory and pushed in batches every 5 seconds.
The Control Layer endpoint defaults to https://api.dexcost.io. To target a
different endpoint (e.g. a local server for testing), set Config::endpoint
explicitly in code:
init.unwrap;
The endpoint is read only from this in-code field — the SDK no longer reads
a DEXCOST_ENDPOINT environment variable, so a hostile process environment
cannot redirect telemetry or the API key.
Features
- LLM Cost Tracking -- Record costs for any LLM provider with auto-pricing from bundled model data
- Non-LLM Cost Tracking -- Track external service costs (APIs, compute, storage)
- Retry Waste Detection -- First-class retry tracking with
is_retry,retry_reason,retry_of - Customer Attribution -- Attribute costs to customers, projects, and workflows
- A/B Testing -- Tag tasks with
experiment_idandvariantfor cost comparison - Nested Tasks -- Link parent/child tasks via task-local context
- Auto-Pricing -- Bundled pricing data for 1000+ LLM models (LiteLLM cost map)
- Custom Pricing -- Override bundled rates or register custom per-1k-token pricing
- Background Sync -- Events buffered in memory and pushed to control layer in batches
- Axum Middleware -- Optional middleware for automatic HTTP request tracking (enable
axum-middlewarefeature)
Custom Pricing
Override bundled LLM pricing:
use pricing_engine;
use dec;
let engine = pricing_engine.unwrap;
let mut engine = engine.lock.await;
engine.set_custom_pricing;
Axum Middleware
Enable the axum-middleware feature in Cargo.toml:
[]
= { = "0.1", = ["axum-middleware"] }
Then add the middleware to your Axum router:
use ;
use dexcost_middleware;
let app = new
.layer;
Key Design Decisions
- All costs use
rust_decimal::Decimal-- never f64 for money - Costs serialized as strings in JSON output for precision
- UUIDs generated via
uuid::Uuid::v4() - Retry waste is a first-class metric (
is_retry,retry_reason,retry_of) - Schema v1 compatible with the Python, TypeScript, and Go SDKs
- Thread-safe:
Arc<Mutex<>>for shared state
Testing
Releases
Releases are generated from Conventional Commit pull-request titles and are
squash-merged to main. Use feat(rust): ... for features and
fix(rust): ... for fixes. See CONTRIBUTING.md.
Privacy
When you connect to the Dexcost Control Layer, the SDK transmits usage data subject to our Privacy Policy.
License
MIT — see LICENSE.