Recursive AI

Estimate LLM prompt tokens offline in C#

OneLLM is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Terminal
dotnet add package RecursiveAI.OneLlm

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneLlm.Counting;
using OneLlm.Messages;
using OneLlm.Providers;
var request = new ChatRequest {
    Messages = [Message.FromUser("Say hello.")]
};
var count = TokenEstimator.Estimate(request);
Console.WriteLine(count.InputTokens);
Console.WriteLine(count.Tier);

The first line is the estimated input size for the sample message. The second line tells you how it was measured. There’s no local model to install or API key to configure.

Unzip the example, then dotnet run.

Text
10
Estimate

Variations

Estimate a string without message overhead. Use this when you want to compare versions of the same text rather than size a whole chat request. Add it after the snippet at the top.

C#
Console.WriteLine(TokenEstimator.EstimateText("Say hello."));
Text
3

Use the model’s catalog settings. Add this after the snippet at the top to estimate that same request using a named model’s family and any charsPerToken override. gpt-6-luna is a model name in OneLLM’s shipped catalog. List the catalog’s model names to pick another. Resolve looks up the model; it doesn’t send the prompt.

C#
var model = new OneLlm.Runtime().Resolve("gpt-6-luna");
Console.WriteLine(TokenEstimator.Estimate(request, model).InputTokens);
Text
10

The short answer

Call TokenEstimator.Estimate(request) for an offline estimate of a OneLLM chat request. Read InputTokens for the number and Tier to confirm it’s an estimate. If you know which model you’ll use, pass its resolved catalog entry as the second argument. You don’t need a model or a tokenizer download to use the generic estimate.

How do LLMs count tokens?

A tokenizer splits text into the pieces a particular model understands. A piece can be a whole word or part of one, so neither a word count nor a character count matches a model’s tokenizer in general. OneLLM’s estimator uses character weights instead of a vocabulary: the counting docs describe the default character ratios by model family and the different treatment for ASCII, two-byte characters, and CJK text. That’s why you shouldn’t scale an estimate for English prose to a prompt of the same length in another language.

The estimator looks at the request rather than only the latest user message. It gathers system text, chat messages, and tool definitions. It adds allowances for each message, tools, and attachments. The difference between the two Say hello. examples is the message and reply setup allowance, not generated output. If your app includes history or tools, estimate the request with those fields included instead of counting only the text in the input box.

How do you count tokens in a prompt before sending it?

Pick the counting method based on what you need. Use the estimate to spot a request that’s getting large while you assemble it. Leave room for the answer and don’t treat the number as a hard guarantee that the request will fit. For example, if you’re choosing how much chat history to keep, size the whole request after selecting the messages, not each user message in isolation.

OneLLM’s three counting tiers distinguish a provider count, a local tokenizer count, and an estimate. The local tokenizer tier counts the text exactly when you supply a matching ILocalTokenizer, but it still estimates the allowances for messages, tools, and files. The provider tier uses a count endpoint where one exists. The code above takes neither of those paths: it always returns Estimate. Use the provider’s usage report for what a completed request actually used, not this preflight number.

All onellm recipes