Record language model token usage
Inspect input, output, total, cache and reasoning counts from one explicit OpenRouter request without treating unavailable values as zero.
// Configure once in Terminal: aner secrets set openrouter
// Checking makes no provider request. Running sends one bounded request and may incur charges.
import aner.llm
import aner.secrets
fn report_tokens(label: String, count: Int64) {
if count < 0 {
print(label + ": unavailable")
} else {
print(label + ": " + String(count))
}
}
let model = llm.client(provider: "openrouter", model: "openai/gpt-4.1-mini",
key: secrets.stored("openrouter"))
let answer = model.generate("Explain the purpose of validation data.", max_tokens: 256)
print("Provider: " + model.provider())
print("Requested model: " + model.model())
print("Returned model: " + answer.model())
print("Finish reason: " + answer.finish_reason())
report_tokens("Input tokens", answer.input_tokens())
report_tokens("Output tokens", answer.output_tokens())
report_tokens("Total tokens", answer.total_tokens())
report_tokens("Cached input tokens (included in input)", answer.cached_input_tokens())
report_tokens("Cache-write input tokens (included in input)", answer.cache_write_input_tokens())
report_tokens("Reasoning tokens (included in output)", answer.reasoning_tokens())
// Details are subsets: do not add them again to input, output or total.
// Provider-reported tokens use that model's tokenizer; they do not verify money charged.
// Store these per-request counts with provider/model identifiers in your experiment log.
Make it your experiment.
With a compatible development runtime installed, save this program as examples/llm_token_usage.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/llm_token_usage.aner
aner run examples/llm_token_usage.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Use the compatible updated native Aner development runtime with Anthropic, Gemini and expanded token usage accessors, plus matching editor integration. Checking makes no inference request and resolves no credential. Running sends one request to the configured provider and can incur charges. Choose an available model and keep the private key in aner.secrets.
aner secrets set openrouter
aner secrets status openrouterClient construction and syntax checking send no inference request. Running this program calls its explicit model service. Inspect the returned finish reason and reported usage; unavailable token counts remain unknown and usage does not verify charges.
Read token accounting and provider requirements