Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneLlm

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

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

C#
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. The output above is from the offline example.

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

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 also distinguish InstanceSpendCap, which counts earlier turns of the same instance.

All onellm recipes