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 during thread deletion when DeleteSessionAsync on the Bedrock Agent runtime returns an AmazonBedrockAgentRuntimeException. The original AWS exception is preserved as InnerException inside AgentThreadOperationException so callers can catch a single type for all thread lifecycle errors while still accessing the AWS-specific error code.
Source
Thrown at dotnet/src/Agents/Bedrock/BedrockAgentThread.cs:86
var endSessionResponse = await this._runtimeClient.EndSessionAsync(
request: new()
{
SessionIdentifier = this.Id
},
cancellationToken: cancellationToken).ConfigureAwait(false);
var deleteSessionResponse = await this._runtimeClient.DeleteSessionAsync(
request: new()
{
SessionIdentifier = this.Id
},
cancellationToken: cancellationToken).ConfigureAwait(false);
this.Id = null;
}
catch (AmazonBedrockAgentRuntimeException 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)
{
// Create the thread if it does not exist. Bedrock agents cannot add messages to the thread without invoking so we don't do that here
await this.CreateAsync(cancellationToken).ConfigureAwait(false);
}
}
View on GitHub (pinned to c028a0c7dc)
Solutions
- Inspect InnerException (AmazonBedrockAgentRuntimeException).ErrorCode to distinguish permission denied, not-found, and throttling.
- Grant `bedrock-agent-runtime:DeleteSession` in the IAM policy for the principal.
- If the session is already gone, treat the error as non-fatal (log and continue) rather than failing the workflow.
- Retry transient throttling with backoff.
Example fix
// before
try { await thread.DeleteAsync(cancellationToken); }
catch (Exception) { /* swallow, hides real cause */ }
// after
try { await thread.DeleteAsync(cancellationToken); }
catch (AgentThreadOperationException ex) when (ex.InnerException is AmazonBedrockAgentRuntimeException aws)
{
if (aws.ErrorCode == "ResourceNotFoundException") return; // already gone
logger.LogError(aws, "Session delete failed: {ErrorCode}", aws.ErrorCode);
throw;
} Defensive patterns
Strategy: try-catch
Validate before calling
// Confirm the session id still exists before deletion (best-effort)
if (string.IsNullOrEmpty(thread.Id)) { /* nothing to delete */ return; } Try / catch
try { await thread.DeleteAsync(cancellationToken); }
catch (AgentThreadOperationException ex) when (ex.InnerException is AmazonBedrockAgentRuntimeException aws)
{
if (aws.ErrorCode == "ResourceNotFoundException") { logger.LogInformation("Session already gone."); return; }
logger.LogError(aws, "DeleteSession failed: {Code}", aws.ErrorCode);
throw;
} Prevention
- Grant `bedrock-agent-runtime:DeleteSession` in the IAM policy.
- Treat ResourceNotFoundException during delete as non-fatal (idempotent cleanup).
- Retry transient throttling with backoff rather than failing the whole workflow.
When it happens
Trigger: Calling DeleteAsync on a BedrockAgentThread whose Id refers to a session the AWS service refuses to delete — missing `bedrock-agent-runtime:DeleteSession` permission, the session was already deleted/expired, or the service is throttling.
Common situations: IAM policy grants Create but not Delete; attempting to delete a session whose identifier has already expired or been removed; cross-account access without delete permission; throttling under heavy load.
Related errors
- The thread could not be created due to an error response fro
- Failed to invoke agent. Status code: {invokeAgentResponse.Ht
- Failed to handle Bedrock Agent stream event.
- Message role must be either Assistant or User.
- runtimeClient
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/916d05f86f7e727e.
Report an issue: GitHub.