Skip to main content

context.ts

Source: sdks/typescript/packages/sdk/src/context.ts. Back to the TypeScript SDK reference index.

note

This page is generated by tools/docs/gen_sdk_reference.py from the SDK sources and the OpenAPI text folders. Do not edit it by hand: change the source and regenerate.

Interface ContextBudgetInit​

Sizes given to the ContextBudget constructor.

Fields:

NameTypeRequiredDescription
windownumberyesW: model context window in tokens.
outputnumberyesB_out: tokens reserved for the answer.
reasoningnumbernoB_reasoning: tokens reserved for internal reasoning.
safetynumbernoB_safety: safety margin in tokens (default 2 percent of the window).
localeLocalenoLanguage of the error messages, en (default) or fr.

Interface BudgetSegment​

A piece of context given to ContextBudget.plan: id, token count, priority and required flag.

Fields:

NameTypeRequiredDescription
idstringyesId of the segment, returned in included or dropped.
tokensnumberyesSize of the segment in tokens.
prioritynumbernoHigher priority segments are kept first (default 0).
requiredbooleannoRequired segments are always kept, an overflow is raised if they do not fit.

Interface BudgetPlan​

Result of ContextBudget.plan: the reserved tokens, the context budget, the tokens used and the kept and dropped segment ids.

Fields:

NameTypeRequiredDescription
windownumberyesContext window of the model in tokens.
output_reservednumberyesTokens reserved for the answer.
reasoning_reservednumberyesTokens reserved for internal reasoning.
safety_marginnumberyesSafety margin in tokens.
context_budgetnumberyesTokens available for the prompt context.
usednumberyesTokens of the kept segments.
remainingnumberyesContext budget left after the kept segments.
includedstring[]yesIds of the segments kept, in input order.
droppedstring[]yesIds of the segments left out, in input order.

Class ContextBudget​

Local Context Manager helper: B_context = W - B_out - B_reasoning - B_safety.

Attributes:

NameTypeRequiredDescription
outputnumberyesTokens reserved for the answer (B_out).
reasoningnumberyesTokens reserved for internal reasoning (B_reasoning).
safetynumberyesSafety margin in tokens (B_safety), 2 percent of the window by default.
windownumberyesContext window of the model in tokens (W).

Constructor ContextBudget​

new ContextBudget(init: ContextBudgetInit)

Rounds the sizes down and checks them; throws RangeError on a negative size and ContextOverflowError when the reserves exceed the window.

NameTypeDefaultRequiredDescription
initContextBudgetInityesWindow, output, reasoning and safety sizes in tokens, and the locale of the messages.

ContextBudget.assertFits​

assertFits(tokens: number): void

Throws ContextOverflowError when the given token count does not fit.

NameTypeDefaultRequiredDescription
tokensnumberyesNumber of tokens to check against the context budget.

Returns: void.

Raises: ContextOverflowError.

ContextBudget.contextBudget​

contextBudget(): number

Tokens available for the prompt context: window minus output, reasoning and safety.

This element takes no parameter.

Returns: number. Context budget in tokens.

ContextBudget.fits​

fits(tokens: number): boolean

Tells whether a token count fits in the context budget.

NameTypeDefaultRequiredDescription
tokensnumberyesNumber of tokens to check.

Returns: boolean. True when tokens is at most the context budget.

ContextBudget.plan​

plan(segments: BudgetSegment[]): BudgetPlan

Greedy selection: required segments first, then by descending priority, keeping input order on ties.

NameTypeDefaultRequiredDescription
segmentsBudgetSegment[]yesSegments to fit; the required ones must fit or ContextOverflowError is thrown.

Returns: BudgetPlan. Budget figures and the kept and dropped segment ids.

Functions​

isEffort​

isEffort(value: unknown): value is Effort

Tells whether a value is one of the efforts none, low, medium, high or max.

NameTypeDefaultRequiredDescription
valueunknownyesValue to check.

Returns: value is Effort. True when the value is a known effort string.

compareEffort​

compareEffort(a: Effort, b: Effort): number

Compares two efforts on the ordered scale none < low < medium < high < max.

NameTypeDefaultRequiredDescription
aEffortyesFirst effort.
bEffortyesSecond effort.

Returns: number. Negative when a is lower than b, 0 when equal, positive when higher.

estimateTokens​

estimateTokens(text: string): number

Rough token estimate (about 4 characters per token) used when no tokenizer is available.

NameTypeDefaultRequiredDescription
textstringyesText to measure.

Returns: number. Length of the text divided by 4, rounded up; 0 for an empty text.