Error class representing a context window overflow in a language model operation.
This error is thrown when the combined input to a language model (such as prompt tokens, historical messages, and/or instructions) exceeds the maximum context window or token limit that the model can process in a single request. Most models have defined upper limits for the number of tokens or characters allowed in a context, and exceeding this limit will prevent the operation from proceeding.
The ContextOverflowError extends the LangChainError base class with
the marker "context-overflow".
class ContextOverflowErrorLangChainError<this>try {
await model.invoke(veryLongInput);
} catch (err) {
if (ContextOverflowError.isInstance(err)) {
// Handle overflow, e.g., prompt user to shorten input or truncate text
console.warn("Model context overflow:", err.message);
} else {
throw err;
}
}The underlying error that caused this ContextOverflowError, if any.
This property is optionally set when wrapping a lower-level error using ContextOverflowError.fromError. It allows error handlers to access or inspect the original error that led to the context overflow.
Creates a new ContextOverflowError instance from an existing error.
This static utility copies the message from the provided error and attaches the original error as the ContextOverflowError.cause property, enabling error handlers to inspect or propagate the original failure.