Skip to content
中文

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 ModelTokenBudget

Field structure ​

FieldTypeRequiredDescription
counterIdstringRequiredIdentifies the tokenizer and its version; recorded in every measurement.
contextWindowTokensnumberRequiredTotal context window of the model, in tokens.
safetyMarginTokensnumber | undefinedOptionalAdditional reserve subtracted together with the output reservation.
countTokens(input: { messages: readonly ModelMessage[]; functions?: ModelOptions["functions"]; json?: boolean; }) => numberRequiredCounts input tokens for the exact request shape that will be sent.

Member index ​

MemberPurpose
countTokensCounts 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; }): number

Counts input tokens for the exact request shape that will be sent.

ParameterTypePassingDescription
input{ messages: readonly ModelMessage[]; functions?: ModelOptions["functions"]; json?: boolean; }Required—
Fields of input
FieldTypeRequiredDescription
messagesreadonly ModelMessage[]RequiredSee the type definition and usage context.
functionsreadonly ModelFunction[] | undefinedOptionalSee the type definition and usage context.
jsonboolean | undefinedOptionalSee the type definition and usage context.

Returns: number.

Examples ​

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);

API reference, usage examples and developer guides.