Table of Contents

Class AIHttpErrorFactory

Namespace
Mythosia.AI.Exceptions
Assembly
Mythosia.AI.Abstractions.dll

Translates a provider's HTTP failure into the library's exception vocabulary.

Every provider words its errors differently, so detection lives here rather than in the core or in the consuming app — the app should only ever learn that the context overflowed, never how a particular vendor phrases it.

Detection is deliberately narrow. A false positive is worse than a miss: it makes the core compact the conversation (which deletes messages irreversibly) in response to an unrelated 400. When in doubt this returns a plain AIServiceException.

public static class AIHttpErrorFactory
Inheritance
AIHttpErrorFactory
Inherited Members

Fields

ContextLengthExceededKey

Metadata key set on a streaming error chunk when the body says the context overflowed.

public const string ContextLengthExceededKey = "context_length_exceeded"

Field Value

string

MaxContextTokensKey

Metadata key carrying the model's context window, when the provider reported it.

public const string MaxContextTokensKey = "max_context_tokens"

Field Value

string

RequestedTokensKey

Metadata key carrying the rejected prompt's size, when the provider reported it.

public const string RequestedTokensKey = "requested_tokens"

Field Value

string

Methods

BuildErrorMetadata(int, string?)

Builds the metadata dictionary for a streaming error chunk.

Streaming keeps yielding an error chunk rather than throwing — that contract is relied on by agent loops and by consumers that render the error as text. This only adds keys, so existing readers are unaffected; the core uses the added flag to decide whether the failure is recoverable.

public static Dictionary<string, object> BuildErrorMetadata(int statusCode, string? errorBody)

Parameters

statusCode int
errorBody string

Returns

Dictionary<string, object>

FromHttp(int, string?, string?, string)

Builds the exception for a failed chat request. Returns ContextLengthExceededException when the body says the prompt did not fit, otherwise a plain AIServiceException.

The message keeps the historical "API request failed ({status}): {reason}" shape so existing log scraping and user-facing strings do not change.

public static AIServiceException FromHttp(int statusCode, string? reasonPhrase, string? errorBody, string messagePrefix = "API request failed")

Parameters

statusCode int
reasonPhrase string
errorBody string
messagePrefix string

Returns

AIServiceException

FromHttp(int, string?, string?, string, bool)

Builds the exception for a failed request and optionally includes the provider body in the exception message. Providers can opt in when their HTTP reason phrase is generic and callers would otherwise lose the actionable diagnostic when logging only Message. The body is always retained in ErrorDetails.

public static AIServiceException FromHttp(int statusCode, string? reasonPhrase, string? errorBody, string messagePrefix, bool includeErrorBodyInMessage)

Parameters

statusCode int
reasonPhrase string
errorBody string
messagePrefix string
includeErrorBodyInMessage bool

Returns

AIServiceException

IsContextLengthExceeded(int, string?, out int?, out int?)

Does this HTTP failure mean "the prompt did not fit"? Only 400 and 413 are considered — 429/5xx are transport or quota problems that compaction cannot fix.

public static bool IsContextLengthExceeded(int statusCode, string? errorBody, out int? maxContextTokens, out int? requestedTokens)

Parameters

statusCode int
errorBody string
maxContextTokens int?
requestedTokens int?

Returns

bool