Add middleware to cap token usage - #3
Merged
Merged
Conversation
There was a problem hiding this comment.
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
TokenCapChatClientmiddleware plus a dedicatedTokenCapExceededExceptionfor enforcing a process-wide token budget. - Wires the middleware into the
IChatClientpipeline 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 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 on lines
+72
to
+75
| if (failed.Data is Exception ex && FindTokenCap(ex) is { } capEx) | ||
| { | ||
| throw capEx; | ||
| } |
Comment on lines
+56
to
+60
| long total = Interlocked.Add(ref _totalTokens, used); | ||
| if (total > maxTotalTokens) | ||
| { | ||
| throw new TokenCapExceededException(total, maxTotalTokens); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.