atiaomar1978-hub opened a new pull request, #24902: URL: https://github.com/apache/camel/pull/24902
## CAMEL-23393 — Agentic loop token budget **Issue:** https://issues.apache.org/jira/browse/CAMEL-23393 **Branch:** `CAMEL-23393-agentic-token-budget` **Commit:** `cad1ca3d939` ### Problem The MCP agentic loop in `OpenAIProducer.processNonStreamingAgentic()` was only capped by `maxToolIterations` (default 50). A loop could still burn through hundreds of thousands of tokens as the full conversation grew on each iteration. ### Solution | Addition | Default | Behavior | |----------|---------|----------| | `maxAgenticTokens` | `0` (unlimited) | Stops the loop when cumulative `prompt + completion` tokens exceed the budget | | `CamelOpenAIAgenticPromptTokens` | — | Cumulative prompt tokens across all agentic iterations | | `CamelOpenAIAgenticCompletionTokens` | — | Cumulative completion tokens across all agentic iterations | | `CamelOpenAIAgenticTotalTokens` | — | Cumulative total across all agentic iterations | Headers are set whenever usage data is available, even if no budget is configured. When the budget is exceeded, an `IllegalStateException` is thrown (similar to `maxToolIterations`) with the iteration number and cumulative token counts. ### Implementation - **`OpenAIAgenticTokenTracker`** — accumulates `usage` from each `ChatCompletion` in the loop - **`OpenAIProducer`** — tracks usage after each API call, sets headers, enforces budget - **OpenAI mock** — chat responses include `usage`; `withUsage(prompt, completion)` added for tests ### Tests (97/97) - **`OpenAIAgenticTokenTrackerTest`** — unit test for token accumulation - **`OpenAIAgenticTokenBudgetTest`** — cumulative headers on successful loop; budget exceeded throws with descriptive message -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
