ModelInputMeasurement
CORE · interface · Type contract
Exact prepared-request accounting; token values exist only with an explicit model counter.
Import
ts
import type { ModelInputMeasurement } from '@jangts/agent-core/model';js
/** @typedef {import('@jangts/agent-core/model').ModelInputMeasurement} ModelInputMeasurement */Other public entry points:@jangts/agent-core, @jangts/agent-core/model, @jangts/agent-core/types, @jangts/agent-core/structs.
Definition
ts
interface ModelInputMeasurementField structure
| Field | Type | Required | Description |
|---|---|---|---|
messageChars | number | Required | Read-only. Characters of the message content. |
instructionChars | number | Required | Read-only. Characters of the instruction text. |
schemaChars | number | Required | Read-only. Characters of the compiled capability schemas. |
transportChars | number | Required | Read-only. Characters the transport itself contributes (for example framing). |
inputChars | number | Required | Read-only. Sum of the character components above. |
maxInputChars | number | Required | Read-only. Character budget the input was compared against. |
maxSchemaChars | number | Required | Read-only. Schema budget the input was compared against. |
maxOutputTokens | number | Required | Read-only. Output tokens reserved for this call, which reduces the token input ceiling. |
counterId | string | undefined | Optional | Read-only. Tokenizer identity; present only when a token budget applies. |
inputTokens | number | undefined | Optional | Read-only. Measured input tokens. |
inputTokenLimit | number | undefined | Optional | Read-only. Token ceiling after reserving output and the safety margin. |
withinBudget | boolean | Required | Read-only. True when every applicable character and token budget accepted the input. |
Examples
- Actual request measurement and same-call preparation snapshot: Use prepare only when context must be handled ahead of time; multiple measurements and the final send reuse the same transport preparation.
Ordinary calls still only use invoke
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 });
console.log((await runner.invoke('你好')).value);js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const runner = new ModelRunner({ provider });
console.log((await runner.invoke('你好')).value);Prepare once only when measurement is needed
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, maxInputChars: 1000 });
const prepared = runner.prepare({ stageId: 'reply', maxOutputTokens: 400 });
const input = '你好,解释这次服务状态';
console.log(prepared.measure(input));
const result = await runner.invoke({ input, prepared });
console.log(result.value);js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const runner = new ModelRunner({ provider, maxInputChars: 1000 });
const prepared = runner.prepare({ stageId: 'reply', maxOutputTokens: 400 });
const input = '你好,解释这次服务状态';
console.log(prepared.measure(input));
const result = await runner.invoke({ input, prepared });
console.log(result.value);