Table of Contents

Class ContextLengthExceededException

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

Thrown when the provider rejects a request because the prompt exceeds the model's context window.

This is usually a recoverable condition: the core AIService catches it, compacts the conversation, and retries. When it does reach the caller, RecoverySkipReason says why recovery could not save it — no conversation policy, nothing left to compact, a tool had already run, the retry budget ran out, and so on. A null reason means recovery never got the chance — the caller invoked the raw GetCompletionAsync(Message) provider overload, which is not routed through it.

StreamAsync never throws this: there the overflow arrives as an error chunk carrying context_length_exceeded metadata, and recovery happens inside the round loop. The legacy callback API StreamCompletionAsync has no round loop and does throw it.

The context limit is owned and enforced by the server, never guessed by the client. That is the whole point of reacting to this exception rather than estimating token counts.

public class ContextLengthExceededException : AIServiceException, ISerializable
Inheritance
ContextLengthExceededException
Implements
Inherited Members

Constructors

ContextLengthExceededException(string, string, int, int?, int?)

public ContextLengthExceededException(string message, string errorDetails, int statusCode, int? maxContextTokens = null, int? requestedTokens = null)

Parameters

message string
errorDetails string
statusCode int
maxContextTokens int?
requestedTokens int?

Properties

MaxContextTokens

The model's context window, when the provider reported it. Null if unknown.

public int? MaxContextTokens { get; }

Property Value

int?

RecoveryAttempts

How many compaction attempts were made before giving up. 0 = never attempted.

public int RecoveryAttempts { get; }

Property Value

int

RecoverySkipReason

Why recovery was skipped or failed, for diagnostics. Null if never attempted.

public string? RecoverySkipReason { get; }

Property Value

string

RequestedTokens

The size of the rejected prompt, when the provider reported it. Null if unknown.

public int? RequestedTokens { get; }

Property Value

int?

StatusCode

HTTP status the provider answered with (400, 413, …).

public int StatusCode { get; }

Property Value

int