Skip to content

Error Handling

ErrorHandlingConfig controls how the agent handles transient errors, retries, and what information gets surfaced to the LLM when a tool or provider call fails.

csharp
var config = new AgentConfig
{
    ErrorHandling = new ErrorHandlingConfig
    {
        MaxRetries = 3,
        SingleFunctionTimeout = TimeSpan.FromSeconds(30)
    }
};

Properties

Retries

PropertyTypeDefaultDescription
MaxRetriesint3Number of retries for transient provider/tool errors
RetryDelayTimeSpan1 secondInitial delay before the first retry (exponential backoff)
MaxRetryDelayTimeSpan30 secondsMaximum backoff delay cap
BackoffMultiplierdouble2.0Exponential backoff multiplier
UseProviderRetryDelaysbooltrueHonour Retry-After headers from the provider
AutoRefreshTokensOn401booltrueAutomatically refresh auth tokens on 401 responses
MaxRetriesByCategoryDictionary<ErrorCategory, int>?nullPer-category retry limits (overrides MaxRetries for that category)
CustomRetryStrategyFunc<Exception, int, CancellationToken, Task<TimeSpan?>>?nullFully custom async retry logic — return null to use the default, return a TimeSpan to wait, or throw to abort

Timeouts

PropertyTypeDefaultDescription
SingleFunctionTimeoutTimeSpan?30 secondsMaximum time allowed for a single tool function call

Error Formatting

PropertyTypeDefaultDescription
NormalizeErrorsbooltrueNormalize provider error responses into a consistent format
IncludeProviderDetailsboolfalseInclude provider-specific error details in the normalized message
IncludeDetailedErrorsInChatboolfalseSend full exception messages to the LLM (security risk — only enable in controlled environments)

Examples

Conservative setup (production)

csharp
ErrorHandling = new ErrorHandlingConfig
{
    MaxRetries = 2,
    RetryDelay = TimeSpan.FromSeconds(2),
    MaxRetryDelay = TimeSpan.FromSeconds(60),
    SingleFunctionTimeout = TimeSpan.FromSeconds(20),
    NormalizeErrors = true,
    IncludeDetailedErrorsInChat = false
}

Debug setup (development)

csharp
ErrorHandling = new ErrorHandlingConfig
{
    MaxRetries = 0,                          // Fail fast
    IncludeDetailedErrorsInChat = true,      // Show full errors to the LLM
    IncludeProviderDetails = true
}

Per-category retry limits

csharp
ErrorHandling = new ErrorHandlingConfig
{
    MaxRetries = 3,
    MaxRetriesByCategory = new Dictionary<ErrorCategory, int>
    {
        [ErrorCategory.RateLimit] = 5,       // More retries for rate limits
        [ErrorCategory.Authentication] = 0   // Never retry auth errors
    }
}

Custom retry strategy

csharp
ErrorHandling = new ErrorHandlingConfig
{
    CustomRetryStrategy = async (exception, attempt, ct) =>
    {
        if (exception is MyTransientException)
            return TimeSpan.FromSeconds(attempt * 2);  // Linear backoff for this type

        return null;  // Fall back to default strategy for everything else
    }
}

JSON Example

json
{
    "ErrorHandling": {
        "MaxRetries": 3,
        "RetryDelay": "00:00:01",
        "MaxRetryDelay": "00:00:30",
        "BackoffMultiplier": 2.0,
        "SingleFunctionTimeout": "00:00:30",
        "UseProviderRetryDelays": true,
        "AutoRefreshTokensOn401": true,
        "NormalizeErrors": true,
        "IncludeProviderDetails": false,
        "IncludeDetailedErrorsInChat": false
    }
}

CustomRetryStrategy and MaxRetriesByCategory cannot be expressed in JSON — set them programmatically on the AgentConfig object.


See Also

Released under the MIT License.