---
title: "Cap LLM cost per request in C#"
url: https://recursiveai.org/onellm/cookbook/limit-llm-spend-before-request/
description: "Cap estimated LLM cost per request in C# before sending it. Set a per-call limit in OneLLM and test the refusal locally without an API key."
version: "1.0.1"
---

# Cap LLM cost per request 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/limit-llm-spend-before-request/limit-llm-spend-before-request.zip)

Set `MaxCostPerCall` to reject a request before sending it. Try it without an API key.

```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;
using OneLlm.Testing;

Runtime runtime = new()
    { MaxCostPerCall = 0.01m };
var provider = new ScriptedProvider("openai");
runtime.Providers.Set("openai", provider);
Llm.Default = runtime;

try
{
    await Llm.AskAsync(
        "gpt-6-luna", "Say hi.");
}
catch (BudgetExceededException error)
{
    Console.WriteLine(error.Message);
}
Console.WriteLine(
    $"Requests: {provider.Requests.Count}");
```

Unzip [the example](https://recursiveai.org/onellm/cookbook/limit-llm-spend-before-request/limit-llm-spend-before-request.zip), then `dotnet run`.

```text
The call could cost up to $0.064001, above the per-call limit of $0.01.
Requests: 0
```

`Requests: 0` means the local scripted provider didn’t receive the chat request. The estimate uses the shipped catalog prices; it isn’t a bill. If the limit can’t be checked because the price or output limit is unknown, the guard refuses the call rather than letting it through.

## Variations

**Give the model a smaller reply allowance.** The same 1-cent limit lets this request through with `MaxOutputTokens = 32`. The reply comes from the scripted provider, not the model.

```csharp
using OneLlm;
using OneLlm.Testing;

var runtime = new Runtime { MaxCostPerCall = 0.01m };
var provider = new ScriptedProvider("openai");
runtime.Providers.Set("openai", provider);
provider.Enqueue(ScriptStep.TextWithUsage("Hi!", 8, 2));

var result = await runtime.AskAsync(new AskRequest
{
    Model = "gpt-6-luna",
    Prompt = "Say hi.",
    MaxOutputTokens = 32
});
Console.WriteLine(result.Text);
```

```text
Hi!
```

**Use the built-in OpenAI provider.** Set `OPENAI_API_KEY` in your environment and keep `MaxCostPerCall = 0.01m` on the runtime. Remove the `ScriptedProvider` and `runtime.Providers.Set(...)` lines so OneLLM uses its [default provider registration](https://recursiveai.org/onellm/docs/happy-path/#providers). The output above is from the offline example.

## Related recipes

- [Track LLM cost per call](https://recursiveai.org/onellm/cookbook/track-llm-cost-per-call/)
- [Test LLM code without an API key](https://recursiveai.org/onellm/cookbook/test-llm-code-without-api-key/)

## The short answer

Set `Runtime.MaxCostPerCall` to a decimal dollar amount. Catch `BudgetExceededException` to handle a refusal before sending the chat request. The check uses an estimate for the whole call, not the charge for the response you expect. The example uses prices from OneLLM’s built-in model catalog.

## How do you estimate LLM API cost before a request?

The [spend-limit docs](https://recursiveai.org/onellm/docs/pricing-and-counting/#estimates-and-spend-limits) describe the estimate as the input cost plus the cost of using the full output allowance. If the request may write to the prompt cache, the estimator uses the dearer of the normal input and cache-write rates. It uses long-context rates when the input crosses the model’s pricing threshold. The rates come from the model catalog, so an old rate or a local override affects the estimate.

A short prompt doesn’t necessarily mean a low estimate. When you don’t specify an output allowance, the estimator uses the model’s maximum. In the example that’s why even “Say hi.” doesn’t fit under one cent. If your task needs only a short reply, use `AskRequest.MaxOutputTokens` as in the variation. Shortening the input alone won’t reduce that output allowance.

The estimate is a local decision, not a provider invoice. To see the cost OneLLM calculates from the usage reported after a successful call, use [per-call cost tracking](https://recursiveai.org/onellm/cookbook/track-llm-cost-per-call/).

## How do you cap total LLM spend instead of the cost of one request?

`MaxCostPerCall` answers “is this one call too expensive?” It does not stop many individually affordable calls. For a total across calls through the same runtime, use `Runtime.SpendCap`. It takes money already spent and reservations for in-flight calls into account before admitting another call. This is a cap for that runtime, not a shared balance across processes or across an API account. The [runtime docs](https://recursiveai.org/onellm/docs/happy-path/) also distinguish `InstanceSpendCap`, which counts earlier turns of the same instance.
