---
title: "Estimate LLM prompt tokens offline in C#"
url: https://recursiveai.org/onellm/cookbook/estimate-prompt-token-count-offline/
description: "Estimate LLM prompt tokens offline in C# before sending a chat request. Count messages as well as text, and distinguish the estimate from provider usage."
version: "1.0.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/estimate-prompt-token-count-offline/estimate-prompt-token-count-offline.zip)

```shell
dotnet add package RecursiveAI.OneLlm
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onellm/cookbook/estimate-prompt-token-count-offline/estimate-prompt-token-count-offline.zip), 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.

```csharp
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](https://recursiveai.org/onellm/cookbook/inspect-model-context-window-offline/) to pick another. `Resolve` looks up the model; it doesn’t send the prompt.

```csharp
var model = new OneLlm.Runtime().Resolve("gpt-6-luna");
Console.WriteLine(TokenEstimator.Estimate(request, model).InputTokens);
```

```text
10
```

## Related recipes

- [Look up a model’s context window offline](https://recursiveai.org/onellm/cookbook/inspect-model-context-window-offline/)
- [Render a prompt before sending it](https://recursiveai.org/onellm/cookbook/render-prompt-template/)

## 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](https://recursiveai.org/onellm/docs/pricing-and-counting/#token-counting) 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](https://recursiveai.org/onellm/docs/pricing-and-counting/#token-counting) 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.
