Skip to content

Chat Context

Defined in: chat/context.ts:192

Stateless context window manager. Takes messages and returns the subset that fits within a token budget.

const manager = new ContextWindowManager({
maxTokens: 4096,
reservedTokens: 500,
strategy: "sliding-window",
});
const result = manager.fitMessages(messages);
// result.messages — trimmed to fit budget
// result.totalTokens — estimated token usage
// result.wasTruncated — whether messages were removed

new ContextWindowManager(config): ContextWindowManager

Defined in: chat/context.ts:198

ContextWindowConfig

ContextWindowManager

get availableBudget(): number

Defined in: chat/context.ts:209

Available token budget after reserving tokens

number

estimateMessageTokens(message): number

Defined in: chat/context.ts:218

Estimate tokens for a single message.

ChatMessage

Message to estimate

number

Estimated token count

fitMessages(messages): ContextWindowResult

Defined in: chat/context.ts:227

Fit messages within the token budget using the configured strategy.

readonly ChatMessage<unknown>[]

All messages to consider

ContextWindowResult

Result with fitted messages and metadata

fitMessagesAsync(messages): Promise<ContextWindowResult>

Defined in: chat/context.ts:265

Async variant of fitMessages that supports async summarization. When strategy is “summarize-placeholder” and a summarizer is configured, calls the summarizer with removed messages and replaces the placeholder text. Falls back to static placeholder if summarizer throws. For other strategies, behaves identically to fitMessages().

readonly ChatMessage<unknown>[]

Promise<ContextWindowResult>

fitMessagesWithUsage(messages, lastPromptTokens, modelContextWindow): ContextWindowResult

Defined in: chat/context.ts:314

Trim messages using real token usage data from the previous API call. Uses average-based algorithm: avgTokensPerMessage = lastPromptTokens / messageCount. Removes oldest non-system messages until freed budget brings usage under modelContextWindow.

readonly ChatMessage<unknown>[]

All messages in the session

number

Real prompt tokens from the last API response

number

Model’s total context window size in tokens

ContextWindowResult

Result with fitted messages and metadata

Defined in: chat/context.ts:155

Context usage statistics for a session. Returned by IChatRuntime.getContextStats().

When real usage data is available (after the first API response), realPromptTokens and realCompletionTokens contain actual token counts. modelContextWindow is the model’s context window from listModels().

availableBudget: number

Defined in: chat/context.ts:163

Available token budget (maxTokens − reservedTokens)

optional modelContextWindow: number

Defined in: chat/context.ts:169

Model’s context window in tokens from listModels() (undefined if not available)

optional realCompletionTokens: number

Defined in: chat/context.ts:167

Real completion tokens from the last API response (undefined before first response)

optional realPromptTokens: number

Defined in: chat/context.ts:165

Real prompt tokens from the last API response (undefined before first response)

removedCount: number

Defined in: chat/context.ts:159

Number of messages removed by trimming

totalTokens: number

Defined in: chat/context.ts:157

Estimated total tokens in the trimmed context (heuristic, kept for backward compat)

wasTruncated: boolean

Defined in: chat/context.ts:161

Whether context was truncated


Defined in: chat/context.ts:99

Configuration for the context window manager.

optional estimation: TokenEstimationOptions

Defined in: chat/context.ts:119

Token estimation options.

maxTokens: number

Defined in: chat/context.ts:101

Maximum token budget for the context window

optional reservedTokens: number

Defined in: chat/context.ts:108

Tokens reserved for system prompt and response generation. Subtracted from maxTokens to get available budget.

0

optional strategy: OverflowStrategy

Defined in: chat/context.ts:114

Strategy for handling overflow when messages exceed budget.

"truncate-oldest"

optional summarizer: ContextSummarizer

Defined in: chat/context.ts:126

Optional async summarizer for the summarize-placeholder strategy. When provided, replaces the static placeholder with a generated summary. Falls back to static placeholder if summarizer throws.


Defined in: chat/context.ts:134

Result of context window trimming.

messages: ChatMessage<unknown>[]

Defined in: chat/context.ts:136

Messages that fit within the budget

removedCount: number

Defined in: chat/context.ts:140

Number of messages removed

totalTokens: number

Defined in: chat/context.ts:138

Total estimated tokens for included messages

wasTruncated: boolean

Defined in: chat/context.ts:142

Whether any messages were truncated


Defined in: chat/context.ts:16

Options for token estimation.

optional charsPerToken: number

Defined in: chat/context.ts:22

Characters per token ratio. Lower = more conservative (fewer messages fit).

4

ContextSummarizer = (removedMessages) => Promise<string>

Defined in: chat/context.ts:94

Async summarizer function for the summarize-placeholder strategy. Receives removed messages and returns a summary string. When configured, replaces the static placeholder text with actual summary.

readonly ChatMessage[]

Promise<string>


OverflowStrategy = "truncate-oldest" | "sliding-window" | "summarize-placeholder"

Defined in: chat/context.ts:82

Overflow strategy type

estimateTokens(message, options?): number

Defined in: chat/context.ts:45

Estimate token count for a single chat message. Uses character-based heuristic: Math.ceil(charCount / charsPerToken).

Counts:

  • Text content (string or text parts)
  • Serialized tool calls and tool results
  • Thinking blocks
  • Role overhead (~4 tokens)

ChatMessage

Chat message to estimate

TokenEstimationOptions

Estimation options

number

Estimated token count

const tokens = estimateTokens(message);
const conservative = estimateTokens(message, { charsPerToken: 3 });