> ## Documentation Index
> Fetch the complete documentation index at: https://cstreams.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Token Costs

> Understanding AI token usage and costs

### What are tokens?

Tokens are the smallest units of text or data that an AI model can process. They can be
words, characters, phrases, or even image segments.

Here's a sample for a paragraph of text:

<div>
  <img src="https://mintcdn.com/cstreams/1iORXuSnPeObgRK8/images/openai-token-sample.png?fit=max&auto=format&n=1iORXuSnPeObgRK8&q=85&s=36aa9bfd44b686ffd115fc2d57161215" alt="Token Cost sample" title="Token Cost sample" width="1414" height="636" data-path="images/openai-token-sample.png" />

  <div className="caption caption--right">
    Token cost sample, from [OpenAI Tokenizer](https://platform.openai.com/tokenizer)
  </div>
</div>

### How are token costs calculated?

Our script composes extracted data points into prompts for processing with OpenAI. API
costs are calculated per million tokens. Costs vary by model and generally decrease over
time as new models are released and competition drives down costs.

We track sent and received tokens with the official OpenAI Tokenizer `tiktoken` [package](https://github.com/openai/tiktoken?tab=readme-ov-file#what-is-bpe-anyway). We then apply the
<a href="/config/ai-models/openai#param-api-pricing" class="param-field-link">API\_PRICING</a>
you set for the active `SELECTED_MODEL_NAME`.

We need to calculate these costs manually because OpenAI does not currently allow
programmatic access to pricing information or actual per-request/response billed costs.

See the [References section](#references) for more information.

### Input Token Estimates

Here's what to expect for token usage:

* **System message**: \~100 tokens (rules and instructions)

* **Schema**: \~300-400 tokens (complex nested JSON structure)

* **PDF metadata**: 50-200 tokens (varies by document), extracted from the PDF

* **OCR text**: \~2,000 per page

* **Vision analysis**: 100-300 tokens, when enabled

The input text character limit is capped by the <a href="/config/ai-models/openai#param-max-ai-input-chars" class="param-field-link">MAX\_AI\_INPUT\_CHARS</a>
setting.

### Output Token Estimates

* **Metadata fields**: \~100 tokens

* **Value, confidence, sources**: \~100 tokens

* **Decisions reasoning object**: \~100 tokens

* **JSON structure overhead**: \~100 tokens

We cap output tokens with the <a href="/config/ai-models/openai#param-max-ai-output-tokens" class="param-field-link">MAX\_AI\_OUTPUT\_TOKENS</a>
setting.

### Reviewing Estimate After Running

Token counts and costs are written to:

1. the terminal
2. the log file
3. the `stats.txt` file

<Warning>
  <b>This is not the actual costs you will be billed</b>, but it's a rough estimate for
  our purposes. Always review the actual costs on your OpenAI Platform account when
  running the script for a large batch of files.
</Warning>

Read more about the script's [outputs](/analysis-and-iteration/evaluation).

### References

* [OpenAI API Pricing](https://openai.com/api/pricing/)
* [How to accurately get the cost of each API call](https://community.openai.com/t/how-to-accurately-get-the-cost-of-each-api-call/578426/3)
* [Request for context total parameter under usage](https://community.openai.com/t/request-for-context-total-parameter-under-usage/838272/8)
* [How to correctly compute the cost of an O1 model API call](https://community.openai.com/t/how-to-correct-compute-the-cost-of-an-o1-model-api-call/1099176/2)
