List<Message> window = new ArrayList<>();
for (int i = history.size() - 1; i >= 0; i--) {
if (countTokens(window) + history.get(i).getTokens() > limit) {
window.add(0, compress(history.get(i)));
break;
}
window.add(0, history.get(i));
}
return window;
The sliding window keeps recent turns intact but the LLM Gateway throws a 413 on POST /api/v2/ai/conversations when the reconstructed payload exceeds the hard limit. We’re trying to pin high-importance system prompts to the front while compressing older messages through a separate summarization endpoint. The token math keeps drifting off by a few hundred, so the request gets rejected before it hits the model. Stuffing the summarized JSON back into a system role messes up the turn order when the loop cuts early. We’ve been adjusting the threshold by hand instead of letting the SDK handle the truncation. The gateway just drops the request after that.
sorry for the newbie question, but the 413 usually hits because the flow pushes the full history array instead of just the active slot values. you’ll avoid the size limit by sending only the matched intent and required fields in the json body, since the dialog engine handles the truncation automatically.