Federico Mariani created CAMEL-23393:
----------------------------------------

             Summary: camel-openai: Add token budget enforcement to the MCP 
agentic loop
                 Key: CAMEL-23393
                 URL: https://issues.apache.org/jira/browse/CAMEL-23393
             Project: Camel
          Issue Type: Improvement
          Components: camel-openai
            Reporter: Federico Mariani


The _camel-openai_ agentic loop (OpenAIProducer.processNonStreamingAgentic()) 
is bounded only by *maxToolIterations* (default 50). This prevents infinite 
iteration but does not guard against token/cost exhaustion. A loop could 
consume hundreds of thousands of tokens across many iterations while the 
iteration count remains within bounds.

Real-world agentic workloads can escalate quickly: each iteration sends the 
full accumulated conversation (system + user + assistant + tool results for 
every prior iteration) back to the model. By iteration 30, the prompt alone may 
exceed 100k tokens even if no single tool returned a large result.

- Add a *maxAgenticTokens* configuration parameter (default: unset / 
unlimited). When set, the loop tracks cumulative `prompt_tokens + 
completion_tokens` across all iterations and stops when the budget is exceeded.
- Expose cumulative token usage via output headers 
(`CamelOpenAIAgenticPromptTokens`, `CamelOpenAIAgenticCompletionTokens`, 
`CamelOpenAIAgenticTotalTokens`) so downstream routes can observe total cost 
regardless of whether the budget is enforced.
- When the budget is exceeded, throw a descriptive exception (similar to the 
existing `maxToolIterations` exceeded behavior) that includes the token count 
and iteration number.

The token data is already available — each `ChatCompletion` response includes 
`usage.promptTokens()` / `usage.completionTokens()`. The implementation only 
needs to accumulate these values across the loop.

*Example usage:*

{code:java}
- to:
    uri: "openai:chat-completion"
    parameters:
      model: "gpt-4o"
      maxToolIterations: 20
      maxAgenticTokens: 200000
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to