LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Browser
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
  • Tools
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Compat
  • Event
  • Llms
  • Profile
  • Stream
  • Structured Output
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Testing
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Standard Schema
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Uuid
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
BrowserUniversalHubNodeLoadSerializableEncoder BackedFile SystemIn MemoryTools
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsCompatEventLlmsProfileStreamStructured OutputLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryTestingToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStandard SchemaStreamTestingTiktokenTypesUuidVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScript@langchain/coreerrorsContextOverflowError
Classā—Since v1.1

ContextOverflowError

Copy
class ContextOverflowError

Bases

LangChainError<this>

Constructors

Properties

Methods

Inherited fromLangChainError

Properties

Pcause: Error
—

The underlying error that caused this ContextOverflowError, if any.

Pmessage: string
—

Human-readable error message.

Pname: stringPstack
View source on GitHub
PisInstance: IsInstanceFn
PstackTraceLimit

Methods

McaptureStackTraceMprepareStackTrace

Example

constructor
constructor
property
cause: Error
property
message: string
property
name: "ContextOverflowError"
property
stack: string
property
isInstance: IsInstanceFn
property
stackTraceLimit: number
method
captureStackTrace
method
fromError→ ContextOverflowError
method
prepareStackTrace

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".

  • Use this error to programmatically identify cases where a user request, prompt, or input sequence is too long to be handled by the target model.
  • Model providers and framework integrations should throw this error if they detect a request cannot be processed due to its size.

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.

Human-readable error message.

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.

Copy
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;
  }
}