tokenTracker()
Wraps a stream with reactive token/cost tracking metadata (Tier 2).
Signature
ts
function tokenTracker<A>(
countTokens: (value: A) => TokenUsage,
opts?: { name?: string },
): StoreOperator<A, A>Parameters
| Parameter | Type | Description |
|---|---|---|
countTokens | (value: A) => TokenUsage | Function that extracts token usage from each emitted value. |
opts | { name?: string } | Optional configuration. |
Returns
StoreOperator<A, A> — pipe-compatible. The returned store has a tokens property (Store<TokenMeta>).
| Method | Signature | Description |
|---|---|---|
get() | () => A \ | undefined |
tokens | Store\<TokenMeta\> | Reactive metadata: promptTokens, completionTokens, totalTokens, cost, count. |
source | callbag | Underlying callbag source for subscriptions. |
Basic Usage
ts
import { state, pipe, effect } from 'callbag-recharge';
import { tokenTracker } from 'callbag-recharge/orchestrate';
const llmOutput = state({ text: 'Hello', usage: { promptTokens: 10, completionTokens: 5 } });
const tracked = pipe(llmOutput, tokenTracker(v => v.usage));
effect([tracked.tokens], () => {
const t = tracked.tokens.get();
console.log(`${t.totalTokens} tokens`); // "15 tokens"
});Options / Behavior Details
- Tier 2: Cycle boundary — each forwarded value starts a new DIRTY+value cycle.
- Accumulative: Token counts and cost accumulate across all values. Resets on reconnect.
- Flexible extraction:
countTokenscan return partial usage — missing fields default to 0.