From 3a938fa1373d68b477e2ccda64cd8743249b1e80 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 19 Jan 2026 13:34:19 +0100 Subject: [PATCH] Handle context window overflow in error handling --- app/MindWork AI Studio/Provider/BaseProvider.cs | 13 ++++++++++++- app/MindWork AI Studio/wwwroot/changelog/v26.1.2.md | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/MindWork AI Studio/Provider/BaseProvider.cs b/app/MindWork AI Studio/Provider/BaseProvider.cs index 4dd27763..0a36674a 100644 --- a/app/MindWork AI Studio/Provider/BaseProvider.cs +++ b/app/MindWork AI Studio/Provider/BaseProvider.cs @@ -164,7 +164,18 @@ public abstract class BaseProvider : IProvider, ISecretId if(nextResponse.StatusCode is HttpStatusCode.BadRequest) { - await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.CloudOff, string.Format(TB("Tried to communicate with the LLM provider '{0}'. The required message format might be changed. The provider message is: '{1}'"), this.InstanceName, nextResponse.ReasonPhrase))); + // Check if the error body contains "context" and "token" (case-insensitive), + // which indicates that the context window is likely exceeded: + if(errorBody.Contains("context", StringComparison.InvariantCultureIgnoreCase) && + errorBody.Contains("token", StringComparison.InvariantCultureIgnoreCase)) + { + await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.CloudOff, string.Format(TB("We tried to communicate with the LLM provider '{0}' (type={1}). The data of the chat, including all file attachments, is probably too large for the selected model and provider. The provider message is: '{2}'"), this.InstanceName, this.Provider, nextResponse.ReasonPhrase))); + } + else + { + await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.CloudOff, string.Format(TB("We tried to communicate with the LLM provider '{0}' (type={1}). The required message format might be changed. The provider message is: '{2}'"), this.InstanceName, this.Provider, nextResponse.ReasonPhrase))); + } + this.logger.LogError("Failed request with status code {ResponseStatusCode} (message = '{ResponseReasonPhrase}', error body = '{ErrorBody}').", nextResponse.StatusCode, nextResponse.ReasonPhrase, errorBody); errorMessage = nextResponse.ReasonPhrase; break; diff --git a/app/MindWork AI Studio/wwwroot/changelog/v26.1.2.md b/app/MindWork AI Studio/wwwroot/changelog/v26.1.2.md index b3d253b6..3240e7d9 100644 --- a/app/MindWork AI Studio/wwwroot/changelog/v26.1.2.md +++ b/app/MindWork AI Studio/wwwroot/changelog/v26.1.2.md @@ -1,6 +1,7 @@ # v26.1.2, build 232 (2026-01-xx xx:xx UTC) - Added the option to hide specific assistants by configuration plugins. This is useful for enterprise environments in organizations. - Added the current date and time to the system prompt for better context in conversations. Thanks Peer `peerschuett` for the contribution. +- Added error handling for context window overflow, which can occur with huge file attachments in chats or the document analysis assistant. - Improved error handling for model loading in provider dialogs (LLMs, embeddings, transcriptions). - Improved the microphone handling (transcription preview) so that all sound effects and the voice recording are processed without interruption. - Improved the handling of self-hosted providers in the configuration dialogs (LLMs, embeddings, and transcriptions) when the host cannot provide a list of models.