{"record":{"id":"f05d6eb82ece57a6","repo":"microsoft/semantic-kernel","slug":"the-message-could-not-be-added-to-the-thread-due-t","errorCode":null,"errorMessage":"The message could not be added to the thread due to an error response from the service.","messagePattern":"The message could not be added to the thread due to an error response from the service\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs","lineNumber":135,"sourceCode":"            throw new AgentThreadOperationException(ErrorMessage, ex);\n        }\n    }\n\n    /// <inheritdoc />\n    protected override async Task OnNewMessageInternalAsync(ChatMessageContent newMessage, CancellationToken cancellationToken = default)\n    {\n        const string ErrorMessage = \"The message could not be added to the thread due to an error response from the service.\";\n\n        // If the message was generated by this agent, it is already in the thread and we shouldn't add it again.\n        if (newMessage.Metadata == null || !newMessage.Metadata.TryGetValue(\"ThreadId\", out var messageThreadId) || !string.Equals(messageThreadId, this.Id))\n        {\n            try\n            {\n                await AgentThreadActions.CreateMessageAsync(this._client, this.Id!, newMessage, cancellationToken).ConfigureAwait(false);\n            }\n            catch (RequestFailedException ex)\n            {\n                throw new AgentThreadOperationException(ErrorMessage, ex);\n            }\n            catch (AggregateException ex)\n            {\n                throw new AgentThreadOperationException(ErrorMessage, ex);\n            }\n        }\n    }\n\n    /// <summary>\n    /// Asynchronously retrieves all messages in the thread.\n    /// </summary>\n    /// <param name=\"sortOrder\">The order to return messages in.</param>\n    /// <param name=\"cancellationToken\">The <see cref=\"CancellationToken\"/> to monitor for cancellation requests. The default is <see cref=\"CancellationToken.None\"/>.</param>\n    /// <returns>The messages in the thread.</returns>\n    /// <exception cref=\"InvalidOperationException\">The thread has been deleted.</exception>\n    [Experimental(\"SKEXP0110\")]\n    public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync(ListSortOrder? sortOrder = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)\n    {","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs#L117-L153","documentation":"Thrown by AzureAIAgentThread.OnNewMessageInternalAsync when AgentThreadActions.CreateMessageAsync fails with a RequestFailedException. This occurs while adding a user/assistant message to the Azure AI thread. The service error is wrapped in AgentThreadOperationException. Note: messages generated by the same agent are skipped (detected via Metadata['ThreadId']), so this only fires for messages that genuinely need to be posted.","triggerScenarios":"Adding a new ChatMessageContent to the thread via the framework's OnNewMessageInternalAsync, and the underlying CreateMessageAsync call to Azure AI Agents fails with a non-404 RequestFailedException. Causes: auth failure, rate limiting, invalid message format, thread not found on service side, or service errors.","commonSituations":"Token expiry during a long conversation. Rate limiting on message creation. Message content too large or in an unsupported format. Thread was deleted out-of-band (on the portal) but not locally. Service transient errors during high-load periods.","solutions":["Inspect the InnerException (RequestFailedException) for the HTTP status and error details.","For 429, implement retry with exponential backoff on the agent invocation.","For 401/403, refresh credentials.","Validate message content size and format before sending.","Ensure the thread has not been deleted externally."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    await agent.InvokeAsync(messages, thread, ct);\n}\ncatch (AgentThreadOperationException ex) when (ex.InnerException is RequestFailedException rfe)\n{\n    _logger.LogError(\"Message add failed: {Status}\", rfe.Status);\n    if (rfe.Status == 429) { /* retry */ }\n    else throw;\n}","preventionTips":["Refresh credentials before long conversations.","Validate message content size limits before sending.","Implement retry policies for 429 responses during agent invocation."],"tags":["azureai-thread","network","service-error","message","agentthreadoperationexception"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}