microsoft/semantic-kernel · error · AgentThreadOperationException

The thread could not be deleted due to an error response fro

Error message

The thread could not be deleted due to an error response from the service.

What it means

Thrown by AzureAIAgentThread.DeleteInternalAsync when DeleteThreadAsync fails with a RequestFailedException whose status is not 404 (404 is silently ignored as the thread was already deleted). The service error is wrapped in AgentThreadOperationException. Common for 401/403 (auth), 429 (rate limit), or 5xx (service) failures during deletion.

Source

Thrown at dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs:113

        }
    }

    /// <inheritdoc />
    protected override async Task DeleteInternalAsync(CancellationToken cancellationToken)
    {
        const string ErrorMessage = "The thread could not be deleted due to an error response from the service.";

        try
        {
            await this._client.Threads.DeleteThreadAsync(this.Id, cancellationToken).ConfigureAwait(false);
        }
        catch (RequestFailedException ex) when (ex.Status == 404)
        {
            // Do nothing, since the thread was already deleted.
        }
        catch (RequestFailedException ex)
        {
            throw new AgentThreadOperationException(ErrorMessage, ex);
        }
        catch (AggregateException ex)
        {
            throw new AgentThreadOperationException(ErrorMessage, ex);
        }
    }

    /// <inheritdoc />
    protected override async Task OnNewMessageInternalAsync(ChatMessageContent newMessage, CancellationToken cancellationToken = default)
    {
        const string ErrorMessage = "The message could not be added to the thread due to an error response from the service.";

        // If the message was generated by this agent, it is already in the thread and we shouldn't add it again.
        if (newMessage.Metadata == null || !newMessage.Metadata.TryGetValue("ThreadId", out var messageThreadId) || !string.Equals(messageThreadId, this.Id))
        {
            try
            {
                await AgentThreadActions.CreateMessageAsync(this._client, this.Id!, newMessage, cancellationToken).ConfigureAwait(false);

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Check the InnerException (RequestFailedException) for the specific HTTP status code.
  2. For 401/403, refresh credentials before attempting deletion.
  3. For 429, retry with backoff.
  4. Note: 404 is already handled gracefully (no-op) — a non-404 error indicates a real problem.
  5. Consider whether the thread was already deleted out-of-band via the Azure portal.
Defensive patterns

Strategy: retry

Try / catch

try
{
    await thread.DeleteAsync(ct);
}
catch (AgentThreadOperationException ex) when (ex.InnerException is RequestFailedException rfe && rfe.Status == 429)
{
    await Task.Delay(TimeSpan.FromSeconds(5), ct);
    await thread.DeleteAsync(ct);
}

Prevention

When it happens

Trigger: Calling DeleteAsync on an AzureAIAgentThread and the Azure AI Agents service rejects the delete request with a non-404 HTTP error. Authentication has expired since the thread was created, or the service is rate-limiting or experiencing an outage.

Common situations: Azure AD token expired between thread creation and deletion. Rate limiting on thread operations. Service degradation. The thread ID is invalid or belongs to a different resource (not 404 but 403). Network interruption during the delete call.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/856f1ff85fd91e76. Report an issue: GitHub.