ModelTokenBudget
CORE · interface · Type contract
Provider-injected token accounting.
Core never guesses a character-to-token conversion, so token budgets exist only when a host supplies a real counter.
Import
ts
import type { ModelTokenBudget } from '@jangts/agent-core/model';js
/** @typedef {import('@jangts/agent-core/model').ModelTokenBudget} ModelTokenBudget */Other public entry points:@jangts/agent-core, @jangts/agent-core/model, @jangts/agent-core/types, @jangts/agent-core/structs.
Definition
ts
interface ModelTokenBudgetField structure
| Field | Type | Required | Description |
|---|---|---|---|
counterId | string | Required | Identifies the tokenizer and its version; recorded in every measurement. |
contextWindowTokens | number | Required | Total context window of the model, in tokens. |
safetyMarginTokens | number | undefined | Optional | Additional reserve subtracted together with the output reservation. |
countTokens | (input: { messages: readonly ModelMessage[]; functions?: ModelOptions["functions"]; json?: boolean; }) => number | Required | Counts input tokens for the exact request shape that will be sent. |
Member index
| Member | Purpose |
|---|---|
countTokens | Counts input tokens for the exact request shape that will be sent. |
Member details
countTokens
Counts input tokens for the exact request shape that will be sent.
ts
ModelTokenBudget.countTokens(input: { messages: readonly ModelMessage[]; functions?: ModelOptions["functions"]; json?: boolean; }): numberCounts input tokens for the exact request shape that will be sent.
| Parameter | Type | Passing | Description |
|---|---|---|---|
input | { messages: readonly ModelMessage[]; functions?: ModelOptions["functions"]; json?: boolean; } | Required | — |
Fields of input
| Field | Type | Required | Description |
|---|---|---|---|
messages | readonly ModelMessage[] | Required | See the type definition and usage context. |
functions | readonly ModelFunction[] | undefined | Optional | See the type definition and usage context. |
json | boolean | undefined | Optional | See the type definition and usage context. |
Returns: number.
Examples
- ModelRunner: input can be shortened while the output contract stays the same: Execute one model interaction through the real Provider port and return a value plus the invocation for this call.
- Bind once, multiple models per run, and switching across runs: Provide stable model configuration at construction; requests select by configuration ID instead of injecting a Provider repeatedly.
- Model strategy: explicit selection and recommended budgets: Select from registered models and reasoning configurations, complete Core settings with local budget tiers, and keep per-field exact overrides.
- Decorators: complete handlers and existing host methods: Classes construct definitions automatically; methods bind the real host. Outputs still use original define, constructor and bind contracts.
Two inputs for the same output
Beginner · Node.js
ts
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const runner = new ModelRunner({ provider });
const first = await runner.invoke('你好');
const second = await runner.invoke({ input: '你好' });
console.log(first.value, second.value);
console.log(first.invocation.invocationId);js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const runner = new ModelRunner({ provider });
const first = await runner.invoke('你好');
const second = await runner.invoke({ input: '你好' });
console.log(first.value, second.value);
console.log(first.invocation.invocationId);Add budget and request cancellation
Configuration · Node.js
ts
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const runner = new ModelRunner({ provider, maxCalls: 2, maxInputChars: 4000,
maxOutputChars: 2000, callTimeoutMs: 15000,
execution: { signal: AbortSignal.timeout(20000) },
});
const result = await runner.invoke({ input: '你好', stageId: 'reply', maxOutputTokens: 600 });
console.log(result.value, runner.remainingCalls);js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const runner = new ModelRunner({ provider, maxCalls: 2, maxInputChars: 4000,
maxOutputChars: 2000, callTimeoutMs: 15000,
execution: { signal: AbortSignal.timeout(20000) },
});
const result = await runner.invoke({ input: '你好', stageId: 'reply', maxOutputTokens: 600 });
console.log(result.value, runner.remainingCalls);