.NETOpenAIC#Cost

How to Count Tokens in C#

Tokens drive LLM cost and context limits. How to count tokens in C# with a tokenizer, estimate cost before a call, and keep prompts under the model's limit.

Everything about working with LLMs is measured in tokens: cost is per token, context windows are sized in tokens, and requests fail when you exceed the limit. So it pays to count them before you send a request, not discover the problem in an error or an invoice. This guide shows how to count tokens in C#.

What a token is

A token is a chunk of text — roughly ¾ of a word on average in English, but it varies. “Hello” might be one token; “internationalization” several. The model sees tokens, not characters or words, so a rough character count is a poor estimate. For anything cost- or limit-sensitive, count properly.

Counting tokens with a tokenizer

Don’t guess — use a real tokenizer that matches your model’s encoding. In .NET, Microsoft.ML.Tokenizers provides tokenizers (including the tiktoken encodings OpenAI uses):

using Microsoft.ML.Tokenizers;

// Get the tokenizer for your model's encoding
Tokenizer tokenizer = TiktokenTokenizer.CreateForModel("gpt-4o-mini");

string text = "How many tokens is this sentence?";
int count = tokenizer.CountTokens(text);

Console.WriteLine($"Tokens: {count}");

Now you have an exact count, not an estimate.

Estimating cost before you call

Once you can count tokens, you can price a request before making it. Cost is (input tokens × input rate) + (output tokens × output rate):

int inputTokens = tokenizer.CountTokens(prompt);
decimal estInputCost = inputTokens / 1000m * inputRatePer1K;
// You won't know output tokens exactly in advance — cap them with max_tokens and price the worst case.

This is invaluable for budgeting and for showing users an estimated cost before an expensive operation.

Staying under the context limit

Every model has a maximum context (input + output tokens). Exceed it and the call fails. Count before sending and trim if needed — this is the core of managing long conversations and large RAG contexts:

if (tokenizer.CountTokens(fullPrompt) > maxContextTokens - reservedForResponse)
{
    // Trim history or retrieved context before calling — see agent memory.
}

Reserve room for the response: if the context limit is 128K and you want up to 4K of output, keep the input under ~124K.

Where this fits

Token counting underpins two things covered elsewhere:

  • Cost control — you can’t optimize spend you don’t measure; token counts are the raw signal.
  • Agent memory — windowing and summarizing conversation history is driven by staying under the token budget.

Note: tokenizer package names and model encodings change; verify against Microsoft.ML.Tokenizers and your provider’s current docs. The principle — count with the model’s real tokenizer, price and trim against the count — is stable.

Takeaway

Tokens are the unit of cost and the hard limit on context, so count them before you call — with a real tokenizer like Microsoft.ML.Tokenizers, not a character estimate. Counting lets you price a request in advance, keep prompts under the context window, and drive the trimming and budgeting that make production AI affordable. It’s a small utility that prevents a lot of surprise errors and invoices.


Have a correction or a topic you want covered? Email mani.bc72@gmail.com.