Skip to content

Add middleware to cap token usage - #3

Merged
JesseLiberty merged 1 commit into
mainfrom
tokenCap
Jul 23, 2026
Merged

Add middleware to cap token usage#3
JesseLiberty merged 1 commit into
mainfrom
tokenCap

Conversation

@JesseLiberty

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings July 23, 2026 18:36
@JesseLiberty
JesseLiberty merged commit 4e1a3b9 into main Jul 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a token-budget enforcement mechanism to the app’s shared IChatClient pipeline so that cumulative model token usage can hard-fail the run and unwind the workflow cleanly once a configured cap is exceeded.

Changes:

  • Introduces TokenCapChatClient middleware plus a dedicated TokenCapExceededException for enforcing a process-wide token budget.
  • Wires the middleware into the IChatClient pipeline and adds top-level handling to exit gracefully when the cap is breached.
  • Ensures workflow/agent error handling rethrows token-cap exceptions so they aren’t swallowed by broad catch (Exception) blocks.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
TokenCapChatClient.cs Adds token-usage tracking middleware and a dedicated exception type.
Program.cs Registers the middleware in the chat-client pipeline and handles cap-breach shutdown at the entry point.
BlogWorkflow.cs Detects token-cap failures in wrapped workflow exceptions and aborts the run.
ReviewerAgent.cs Ensures token-cap exceptions propagate rather than being handled as generic failures.
ResearcherAgent.cs Ensures token-cap exceptions propagate rather than being handled as generic failures.
AuthorAgent.cs Ensures token-cap exceptions propagate rather than being handled as generic failures.
BloggerAgent.cs Ensures token-cap exceptions propagate rather than being handled as generic failures.

Comment thread Program.cs
Comment on lines +41 to 45
// TokenCapChatClient is registered *after* function invocation, which makes it
// the innermost wrapper around the raw client — so it observes every individual
// model round-trip (including the extra calls tool invocation triggers) and
// enforces a hard 1000-token budget for the whole process.
IChatClient llm = openAIClient
Comment thread BlogWorkflow.cs
Comment on lines +72 to +75
if (failed.Data is Exception ex && FindTokenCap(ex) is { } capEx)
{
throw capEx;
}
Comment thread TokenCapChatClient.cs
Comment on lines +56 to +60
long total = Interlocked.Add(ref _totalTokens, used);
if (total > maxTotalTokens)
{
throw new TokenCapExceededException(total, maxTotalTokens);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants