Error Handling Middleware
Error handling is split across wrapper middleware, iteration safety middleware, events, and normal application exception handling.
For UI rendering and event capture, see Lifecycle, Retry, And Error Events.
OnErrorAsync is the terminal failure observer for middleware execution. It runs exactly once for model, function, iteration, message-turn, input, runtime, and thread-fork failures. Caller cancellation stays on the cancellation lifecycle and does not invoke OnErrorAsync.
Choose The Boundary
Use the boundary that matches the failure:
| Failure | Primary mechanism | Notes |
|---|---|---|
| Transient model/provider streaming error | RetryMiddleware model wrapper, then ErrorFormattingMiddleware model wrapper | On final failure, OnErrorAsync receives ErrorSource.ModelCall; ModelCallRetryEvent can arrive after partial text streamed. |
| Function body exception | Function wrappers, MiddlewareErrorEvent, OnErrorAsync, then AfterFunctionAsync formatting | OnErrorAsync receives ErrorSource.ToolCall before result formatting. |
| Slow function | FunctionTimeoutMiddleware | Put timeout inside retry for per-attempt timeout. |
| Repeated tool errors or loops | ErrorTrackingMiddleware, TotalErrorThresholdIterationMiddleware, CircuitBreakerMiddleware | These are iteration safety policies, not exception formatters. |
| Whole turn failure | OnErrorAsync, MessageTurnErrorEvent, and the exception from RunAsync(...) | OnErrorAsync receives ErrorSource.MessageTurn; the original failure still propagates. Client setup failures also expose MessageTurnErrorEvent.ClientSetup and implement IClientSetupFailure at the direct-call boundary. |
Recommended Wrapper Order
For function/model wrapper behavior, first registered is outermost. The recommended stack is:
RetryMiddleware(FunctionTimeoutMiddleware(ErrorFormattingMiddleware(core)))That means retry wraps timeout, and error formatting sits closest to the core call.
Each of these is a setting on the agent's definition. The AgentConfig helpers write the policy; the runtime derives the middleware from that agent's own definition when it builds the agent, in the recommended order, so a policy set on one agent never runs for another:
using HPD.Agent;
using HPD.Agent.Providers.OpenAI;
var assistant = new AgentConfig()
.WithOpenAI(model: "gpt-5-mini")
.WithRetry(retry =>
{
retry.ModelRetries.MaxRetries = 2;
retry.FunctionRetries.MaxRetries = 2;
retry.FunctionRetries.RetryDelay = TimeSpan.FromSeconds(1);
})
.WithFunctionTimeout(TimeSpan.FromSeconds(10))
.WithErrorFormatting(includeDetailedErrors: false);
await using var runtime = await new AgentRuntimeBuilder()
.AddAgent("assistant", assistant)
.BuildAsync();| Helper | Definition setting | Derived middleware |
|---|---|---|
WithRetry(...) | Resilience.Retry | RetryMiddleware |
WithFunctionTimeout(...) | Resilience.FunctionTimeout | FunctionTimeoutMiddleware, inside retry |
WithErrorFormatting(...) | ErrorHandling | ErrorFormattingMiddleware, which every agent gets, with secure defaults when ErrorHandling is not set |
A definition read from JSON, YAML, or the agent store gets the same middleware from the same settings.
Functions opt into automatic retry explicitly:
[AIFunction(Replay = ReplayPolicy.Idempotent)]
public string LookUpOrder(string id) => orders.Find(id);Omitting Replay also means Never; unsafe functions need no annotation. Write Never explicitly only when overriding an inherited or function-level safe policy.
Retry is explicit. A function must resolve to ReplayPolicy.Idempotent through its function or action declaration, and application exclusions always win; a positive budget does not make a function replay-safe. WithRetry(...) can be called more than once; each call adjusts the same Resilience.Retry section.
Custom retry strategies are process-local parts of the definition: WithFunctionRetryStrategy(...) and WithModelRetryStrategy(...) set delegates on Resilience. They take effect when retry is enabled on the definition; setting a strategy alone does not enable retry. A definition carrying them cannot be stored.
When the runtime builds the agent, the derived retry and formatting middleware receive the error handler of the agent's provider, so classification and retry-after parsing are provider-aware.
Retry Events
RetryMiddleware wraps both model streaming and function calls.
Function retries emit FunctionRetryEvent. Model-call retries emit ModelCallRetryEvent.
For streaming UIs, handle ModelCallRetryEvent carefully. The event can arrive after partial assistant text has already been displayed. A UI should clear or mark the partial text before showing fresh retry content, otherwise users may see text from a failed attempt mixed with text from the retry.
using var retryEvents = runtime.Subscribe<ModelCallRetryEvent>(evt =>
{
Console.WriteLine(
$"Model retry {evt.Attempt}/{evt.MaxRetries} after {evt.Delay.TotalMilliseconds:N0} ms");
// Streaming UIs should clear or mark partial assistant text here.
});Function retry events are simpler because the function result is not streamed token-by-token:
using var functionRetries = runtime.Subscribe<FunctionRetryEvent>(evt =>
{
Console.WriteLine(
$"{evt.FunctionName} retry {evt.Attempt}/{evt.MaxRetries}: {evt.ExceptionType}");
});Function retries require effective ReplayPolicy.Idempotent metadata, no matching exclusion, and an available function budget. Model retries use ModelRetries independently. Each scope can configure a custom strategy; otherwise retry asks the provider error handler for classification and delay, including retry-after guidance when available, then falls back to exponential backoff with jitter. Non-retryable errors, exclusions, and exhausted budgets propagate.
Exclusions use exact ordinal names and always win over metadata:
assistant.WithRetry(retry => retry.FunctionRetries.Exclude = new()
{
Harnesses = ["ComputerHarness"],
Functions = ["SendPayment"],
Actions =
[
new ActionRetryExclusion
{
Function = "Files",
Action = "delete"
}
]
});An exclusion does not grant permission, change timeout behavior, or affect model retries.
Function Timeout
FunctionTimeoutMiddleware wraps function execution and enforces the configured single-function timeout. It is a function wrapper, so ordering matters.
Function timeouts are opt-in. Resilience.FunctionTimeout defaults to null, so ordinary functions—including synchronous subagent tools—run until they complete or the caller cancels them. Set Resilience.FunctionTimeout or call .WithFunctionTimeout(...) on the agent's definition when an application needs a deadline. The parameterless .WithFunctionTimeout() helper uses the configured value when present and otherwise applies its explicit 30-second convenience default.
Place timeout inside retry if you want each retry attempt to receive its own timeout.
Timeout only wraps function execution. It does not impose a whole conversation timeout and it does not cancel model streaming.
Error Formatting
ErrorFormattingMiddleware formats function exceptions in AfterFunctionAsync and sanitizes model-call errors in a model streaming wrapper.
Set IncludeDetailedErrorsInChat = false for production-facing agents unless the tool errors are already sanitized. With detailed errors disabled, function errors sent back into the model are generic. The original exception remains available to middleware context and observability paths.
ErrorHandlingConfig.IncludeDetailedErrorsInChat defaults to true. With detailed errors enabled, exception messages may be exposed to the model and user. Set the option to false for production-facing agents when tool exception text can contain secrets, filesystem paths, connection strings, or internal implementation details.
For model-call errors, formatting rethrows an InvalidOperationException with either a sanitized provider-aware message or a detailed message, depending on the same setting.
Iteration Safety Middleware
Error tracking, total error threshold, and circuit breaker middleware are iteration/tool safety policies. They use middleware state to track failures and can skip work or terminate runs according to policy.
These policies should use UpdateMiddlewareState<TState>(...) for single-state updates and UpdateState(...) when changing core loop state such as termination:
context.UpdateState(state => state with
{
IsTerminated = true,
TerminationReason = "Circuit breaker opened"
});Common definition helpers:
var assistant = new AgentConfig()
.WithOpenAI(model: "gpt-5-mini")
.WithCircuitBreaker(maxConsecutiveCalls: 3)
.WithErrorTracking(maxConsecutiveErrors: 3);They write Resilience.CircuitBreaker, Resilience.ConsecutiveErrors, and (for WithTotalErrorThreshold(...)) Resilience.TotalErrors; the runtime derives each agent's iteration safety middleware from those settings. A custom error detector set with WithErrorDetector(...) is a process-local part of the definition.
Use circuit breaker for repeated identical tool calls. Use error tracking for consecutive tool failures. Use total error threshold when the aggregate error count across a run should stop further work.
OnErrorAsync Scope
Use OnErrorAsync as the single terminal error observer. Each exception is reported once, even when it crosses multiple execution boundaries.
ErrorSource | Failure boundary |
|---|---|
ModelCall | Provider resolution, model request, or final model-stream failure |
ToolCall | Function-body failure |
Iteration | Iteration hook, function-wrapper, or agent-loop processing failure |
MessageTurn | Turn preparation, turn hook, finalization, or persistence failure |
Input | Input dispatch or input middleware/handler failure |
Runtime | Runtime start, stop, or resource lifecycle failure |
ThreadFork | Fork preparation or BeforeThreadForkCommitAsync failure |
ErrorContext.RunConfig exposes the run configuration when available. Source flags such as IsModelError, IsInputError, and IsThreadForkError avoid string or enum checks in common handlers.
OnErrorAsync runs in reverse middleware registration order. Exceptions thrown by an error handler are swallowed so they cannot replace the original failure. The original exception continues to propagate after all handlers complete.
Common Errors
Use these symptoms to pick the fix:
| Symptom | Likely cause | Fix |
|---|---|---|
| Users see partial text followed by unrelated retry text | UI ignored ModelCallRetryEvent | Clear or mark the partial response when the event arrives. |
| Function timeout retries take longer than expected | Timeout is inside retry, so each attempt gets a full timeout or the tool is not eligible for retry | Lower Resilience.FunctionTimeout, lower FunctionRetries.MaxRetries, or add an application-level deadline. |
| Sensitive exception text appears in chat | Detailed errors are enabled or the tool returns sensitive strings itself | Set IncludeDetailedErrorsInChat = false and sanitize tool return values. |
OnErrorAsync receives a formatted model error | ErrorFormattingMiddleware finished before the terminal error boundary | Inspect ErrorContext.Error.InnerException when the original provider exception is required. |
| Retry policy ignores provider-specific behavior | A RetryMiddleware instance was constructed by hand without a provider handler | Configure retry on the definition with WithRetry(...), so the runtime supplies the agent's provider error handler. |
| Retry configured for one agent does not run for another | Retry is a per-agent definition setting | Call WithRetry(...) on each definition that needs it. |
What Not To Overclaim
Error middleware improves recovery and presentation; it is not a transactional guarantee. A function that partially mutates an external system before throwing may be retried, so write non-idempotent tools with their own idempotency keys or confirmation boundaries.
Timeout cancellation depends on the function honoring cancellation or completing the awaited task. It prevents the middleware from waiting past the timeout, but it cannot guarantee cleanup of external side effects.