Loading...
const tokens = text.length / 4;
const cost = tokens * PRICE_PER_TOKEN;const { input_tokens, output_tokens } = response.usage;
const cost =
(input_tokens / 1e6) * INPUT_PRICE_PER_M +
(output_tokens / 1e6) * OUTPUT_PRICE_PER_M;Real token counts from the API response with separate input and output prices give the actual cost, not a rough guess.
Generating each output token takes a separate step of the model, while input tokens are processed together, so output needs more compute.
Trim chat history, cache repeated prompt prefixes, set a maximum output length, and use a smaller model for simple tasks.
Yes. With reasoning models, internal reasoning tokens are usually billed as output tokens, even when they are not shown.