i set this up to use gemini pro 1.5 and it literally filled up its 1 million context window, costing me $1.25. wtf? how does it even decide what context to send?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This behavior is likely tied to how context management is handled in bolt.diy when interacting with LLMs, including Gemini Pro 1.5. Let's break this down:
1. Why Did It Use So Much Context?
The cost and large context usage are due to how the application decides what to include in the context it sends to the model. Based on the provided code and documentation, we can infer the following:
Context Management Code
The useMessageParser hook in useMessageParser.ts appears to handle parsing and managing assistant messages. It uses the StreamingMessageParser to extract relevant data from messages.
However, there is no explicit mechanism in the provided code that limits the size of the context or trims it when it exceeds a certain token limit.
Request Construction
In api.llmcall.ts, the llmCallAction function constructs the context for the LLM:
It appears that the user input (message) is sent directly without any trimming or summarization. Additionally, if the system prompt or other context (e.g., history) is included, it could bloat the context window.
Dynamic Token Limit
The application supports dynamic token limits for LLMs. In llmCallAction, the token limit for Gemini Pro 1.5 is determined dynamically:
If Gemini Pro 1.5 has a very high token limit (e.g., 1 million tokens), the app will not enforce trimming or summarization unless explicitly configured.
2. Why Did This Happen Specifically with Gemini Pro 1.5?
Gemini Pro 1.5 has a 1 million token context window, which is significantly larger than most other models (e.g., OpenAI GPT models typically max out at 32k tokens). Without safeguards, the app likely sent:
User input: Your full prompt.
System prompt and history: Any prior messages or context that the app thought were relevant.
This lack of context trimming caused the model to process all of it, filling up the entire 1 million token window.
3. How to Prevent This in the Future
To avoid excessive token usage and high costs, the following strategies can be applied:
a. Summarize or Trim Context
The app should summarize or trim past messages to reduce the context size. This can be implemented in llmCallAction by processing the messages array before sending it to the model. For example:
Keep only the most recent messages.
Summarize older messages into a smaller representation.
b. Set a Hard Limit on Tokens
Even though Gemini Pro supports 1 million tokens, you can configure the app to use a smaller portion of the context window. Modify the dynamicMaxTokens logic in llmCallAction:
Replace YOUR_MAX_DESIRED_TOKENS with a value like 8192 or 32,768 to cap the token usage.
c. Use Context-Aware Prompts
If the model is used to generate code, responses, or other artifacts, focus the prompt on the specific task. Avoid including unnecessary details from prior interactions.
d. Debug Context Sent
Enable logging in the app to see exactly what is being sent to the model. The createScopedLogger utility in the app can help trace the context:
ts
logger.info('Context sent to model:', messages);
e. Use Smaller Models When Possible
If your task doesn’t require the extensive context provided by Gemini Pro 1.5, switch to a smaller model with fewer tokens and lower costs.
4. Recommended Changes to the Code
Here’s how you can patch this issue directly in api.llmcall.ts:
ts
// Ensure context stays within a reasonable size
function trimContext(messages: any[], maxTokens: number): any[] {
let tokenCount = 0;
const trimmedMessages = [];
for (let i = messages.length - 1; i >= 0; i--) {
const messageTokens = countTokens(messages[i].content); // A hypothetical token counter function
if (tokenCount + messageTokens > maxTokens) break;
trimmedMessages.unshift(messages[i]);
tokenCount += messageTokens;
}
return trimmedMessages;
}
// Inside llmCallAction
const trimmedMessages = trimContext([
{ role: 'system', content: system },
{ role: 'user', content: message }
], YOUR_MAX_DESIRED_TOKENS);
5. Conclusion
The issue arises because the app doesn't currently enforce any limits or trimming for models with large context windows like Gemini Pro 1.5. To prevent high costs:
Summarize or trim context dynamically.
Set a hard token limit.
Monitor context sent to models.
By implementing these safeguards, you can ensure better cost control and prevent filling up unnecessarily large context windows.