This document defines “storage/aggregation required as a reproduction implementation”.
- Aggregation of Token usage (required)
- cost calculation (optional)
- Save tool output cache (optional)
- Usage is accumulated for all LLM calls of the Agent and can be referenced with
getUsage() - Use usage to determine compaction
export type UsageSummary = {
total_calls: number;
total_tokens: number;
total_input_tokens: number;
total_output_tokens: number;
total_cached_input_tokens: number;
total_cache_creation_tokens: number;
total_cost_usd?: number | null;
by_model: Record<string, {
calls: number;
input_tokens: number;
output_tokens: number;
cached_input_tokens: number;
cache_creation_tokens: number;
total_tokens: number;
cost_usd?: number | null;
}>;
};- Record if you can get
response.usageevery timellm.ainvoke() total_calls/by_model[].callsonly increase usage for calls that can be obtained
The Python version retrieves price data only when include_cost is true and caches it for one day.
Same for TS version:
- If
includeCost=false, no external acquisition or caching (zero side effects) - For
includeCost=true, cost can be calculated by obtaining price information fromPricingProvider
export type ModelPricing = {
model: string;
input_cost_per_token?: number | null;
output_cost_per_token?: number | null;
max_tokens?: number | null;
max_input_tokens?: number | null;
max_output_tokens?: number | null;
cache_read_input_token_cost?: number | null;
cache_creation_input_token_cost?: number | null;
};
export interface PricingProvider {
getModelPricing(model: string): Promise<ModelPricing | null>;
}TokenCost receives PricingProvider in DI, and if there is none, it operates without cost.
- 1 day cache (follows the Python version)
- Decide the save destination according to the “implementation environment” (for example,
~/.cache/...for Node) - Even if the cache is corrupted, it will work without cost (prioritize robustness)
tool output cache is a storage area for redeploying with reference ID. Node's CLI implementation may provide ``save files in directories''.
Requirements:
- The agent continues to operate even if the save fails (log only)
- Saved contents can be uniquely identified by
ref_id