ModelTokenBudget
CORE · interface · 类型合同
Provider-injected token accounting.
Core never guesses a character-to-token conversion, so token budgets exist only when a host supplies a real counter.
导入
ts
import type { ModelTokenBudget } from '@jangts/agent-core/model';js
/** @typedef {import('@jangts/agent-core/model').ModelTokenBudget} ModelTokenBudget */其他公开入口:@jangts/agent-core、@jangts/agent-core/model、@jangts/agent-core/types、@jangts/agent-core/structs。
定义
ts
interface ModelTokenBudget字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
counterId | string | 必需 | Identifies the tokenizer and its version; recorded in every measurement. |
contextWindowTokens | number | 必需 | Total context window of the model, in tokens. |
safetyMarginTokens | number | undefined | 可省略 | Additional reserve subtracted together with the output reservation. |
countTokens | (input: { messages: readonly ModelMessage[]; functions?: ModelOptions["functions"]; json?: boolean; }) => number | 必需 | Counts input tokens for the exact request shape that will be sent. |
成员目录
| 成员 | 用途 |
|---|---|
countTokens | Counts input tokens for the exact request shape that will be sent. |
成员详情
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | { messages: readonly ModelMessage[]; functions?: ModelOptions["functions"]; json?: boolean; } | 必需 | — |
input 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
messages | readonly ModelMessage[] | 必需 | 见类型定义及调用场景。 |
functions | readonly ModelFunction[] | undefined | 可省略 | 见类型定义及调用场景。 |
json | boolean | undefined | 可省略 | 见类型定义及调用场景。 |
返回: number。
使用示例
- ModelRunner:输入可以省写,输出合同保持一致:用真实 Provider 端口执行一次模型交互,返回 value 和本次 invocation。
- 一次绑定,单次多模型与跨次切换:构造时提供稳定模型配置;请求用配置 ID 选择,不反复注入 Provider。
- 模型策略:显式选择与推荐预算:从已登记的模型和推理配置中选择,以本地预算档位补全 Core 设置,并保留逐字段精确覆盖。
- 装饰器:处理类与已有实例方法:类装饰代办定义实例化,方法装饰绑定原对象;输出仍走原 define、构造和 bind。
同一输出的两种输入
入门 · 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);增加预算与请求取消
配置 · 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);