# API
```rust
use std::collections::BTreeMap;
use kcode_k1_accounting::UsageValue;
use serde_json::Value;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error { /* private fields */ }
impl std::fmt::Display for Error;
impl std::error::Error for Error;
#[derive(Clone, Debug)]
pub struct UsageAccumulator { /* private fields */ }
impl UsageAccumulator {
pub fn new() -> Self;
pub fn apply(&mut self, value: &Value) -> Result<(), Error>;
pub fn reconciled_rounds(&self) -> usize;
pub fn snapshot(&self) -> BTreeMap<String, UsageValue>;
}
```
`UsageAccumulator` owns all state. Independent accumulators proceed concurrently; one accumulator follows ordinary Rust `&mut` exclusivity. The library performs no I/O, retries, callbacks, background work, or synchronization.
`apply` accepts a JSON object containing `total` and `last` objects whose five token fields are nonnegative `u64` values. The first update requires equal totals. A changed cumulative total requires its checked component-wise delta to equal `last`; an exact duplicate is accepted without adding a round.
Each nonduplicate round accumulates `input tokens`, `cached input tokens`, `output tokens`, and `reasoning tokens`. Input excludes cached tokens and includes ordinary plus cache-write input; output excludes reasoning. Prices are exact decimal cents and use the high tier only when that round's input exceeds 272,000. Impossible token relationships fail.
`snapshot` returns an owned copy and contains all four keys after the first accepted round, including a zero-valued round. Work is linear in the fixed five-field input and the four accumulated entries; retained memory is constant apart from those entries and owned snapshots. The reference canary `cargo test --release --test usage accumulator_canary` applies 100,000 consecutive valid updates in under one second on stable Rust, Linux x86-64, four 3.0 GHz-or-faster cores, and 8 GiB RAM; debug builds use five seconds.