Federico Mariani created CAMEL-23394:
----------------------------------------

             Summary: camel-openai: Add conversation history size management to 
prevent context overflow
                 Key: CAMEL-23394
                 URL: https://issues.apache.org/jira/browse/CAMEL-23394
             Project: Camel
          Issue Type: Improvement
            Reporter: Federico Mariani


When _conversationMemory=true_, conversation history accumulates in the 
exchange property `CamelOpenAIConversationHistory` with no size limit. In 
long-running exchanges (e.g., a route that loops multiple user prompts through 
the same exchange), the history grows until it exceeds the model's context 
window, causing API errors.

This is particularly a potential issue with the agentic loop, where each tool 
iteration adds both the assistant message (with tool calls) and every tool 
result message to the history. A single agentic exchange with 10 iterations 
could add 20+ messages to the history.

- Add a *maxHistoryMessages* parameter (default: unset / unlimited). When set, 
only the N most recent messages are retained in the history. System and 
developer messages are excluded from the count since they are prepended 
separately by `buildMessages()`.
- Add a *maxHistoryTokens* parameter (default: unset / unlimited) for 
token-aware trimming. This would use a simple heuristic (e.g., chars / 4).
- When trimming occurs, log a DEBUG message indicating how many messages were 
dropped.

The sliding window approach is simple and predictable. A more advanced 
summarization-based compaction (calling the model to summarize old history) 
could be added later as a separate feature, but the sliding window addresses 
the immediate risk of unbounded growth.

*Example usage:*

{code:java}
- to:
    uri: "openai:chat-completion"
    parameters:
      model: "gpt-4o"
      conversationMemory: true
      maxHistoryMessages: 50
{code}



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

Reply via email to