# ContextOverflowError

> **Class** in `@langchain/core`

📖 [View in docs](https://reference.langchain.com/javascript/langchain-core/errors/ContextOverflowError)

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

## Signature

```javascript
class ContextOverflowError
```

## Description

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

## Extends

- `LangChainError<this>`

## Constructors

- [`constructor()`](https://reference.langchain.com/javascript/langchain-core/errors/ContextOverflowError/constructor)

## Properties

- `cause`
- `message`
- `name`
- `stack`
- `isInstance`
- `stackTraceLimit`

## Methods

- [`captureStackTrace()`](https://reference.langchain.com/javascript/langchain-core/errors/ContextOverflowError/captureStackTrace)
- [`fromError()`](https://reference.langchain.com/javascript/langchain-core/errors/ContextOverflowError/fromError)
- [`prepareStackTrace()`](https://reference.langchain.com/javascript/langchain-core/errors/ContextOverflowError/prepareStackTrace)

## Examples

```typescript
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;
  }
}
```

---

[View source on GitHub](https://github.com/langchain-ai/langchainjs/blob/e493ed653d734e31a8d051f78ab29b067f530e4b/libs/langchain-core/src/errors/index.ts#L186)