Repository navigation
Putting a dollar cap on one Semantic Kernel agent run (Python) #14529
Replies: 1 comment
|
The same works from .NET (tested with Semantic Kernel 1.80.1 on .NET 10). Run the gateway as a separate process ( var http = new HttpClient(new RunBudgetHandler { InnerHandler = new HttpClientHandler() });
var openai = new OpenAIClient(new ApiKeyCredential("unused"), new OpenAIClientOptions
{
Endpoint = new Uri("http://127.0.0.1:8000/v1"),
Transport = new HttpClientPipelineTransport(http),
});
builder.AddOpenAIChatCompletion("gpt-4o-mini", openai);
The full handler and scope code is in dotnet/extensions#7802. |
Uh oh!
There was an error while loading. Please reload this page.
With auto function calling, one Semantic Kernel invocation can loop through many model calls, and the cost of a single request isn't bounded. Here's one way to give each run a hard dollar budget without changing your kernel or plugins.
Give the chat service an
AsyncOpenAIclient that carries the run's id and budget as headers, pointed at a small gateway running in the same process:Each call reserves its worst-case cost before it's sent, so parallel function calls in one run can't overspend together. A call that doesn't fit is refused with a 402 before the provider and shows up as
ServiceResponseException.inferrail work <run id>shows what the run cost.Caveats: Chat Completions only, set max tokens in your execution settings, and only calls through that service count. Tested with
semantic-kernel1.44.1 andinferrail0.4.10 or later (earlier versions conflicted with SK'smcppin).Full setup: guide. I maintain Inferrail (open source, Apache-2.0), so take the recommendation with that in mind. Curious how others bound the cost of a single run here.
All reactions