PreparedModelCall
CORE · interface · Type contract
Request-bound, single-use transport snapshot.
Repeated measurements do not prepare or spend calls.
Import
import type { PreparedModelCall } from '@jangts/agent-core/model';/** @typedef {import('@jangts/agent-core/model').PreparedModelCall} PreparedModelCall */Other public entry points:@jangts/agent-core, @jangts/agent-core/model, @jangts/agent-core/types, @jangts/agent-core/structs.
Definition
interface PreparedModelCall<Result>Extends or implements: PreparedModelTransport<Result>.
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Result | — | unknown |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
modelId | string | undefined | Optional | Read-only. Model binding the call was prepared with. |
stageId | string | Required | Read-only. See the type definition and usage context. |
transportId | string | Required | Read-only. See the type definition and usage context. |
maxOutputTokens | number | Required | Read-only. See the type definition and usage context. |
remainingCalls | number | Required | Read-only. Calls still available; a live value, so later spends are visible. |
measure | (input: string | readonly ModelMessage[] | BuiltContext) => ModelInputMeasurement | Required | Preflight measurement of one input shape. Never marks the call used and never charges the budget. |
options | Pick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls" | "temperature"> | Required | Read-only. See the type definition and usage context. |
schemaChars | number | Required | Read-only. See the type definition and usage context. |
instruction | string | undefined | Optional | Read-only. See the type definition and usage context. |
createResponse | () => ModelResponseCollector<Result> | Required | See the type definition and usage context. |
Member index
| Member | Purpose |
|---|---|
measure | Preflight measurement of one input shape. Never marks the call used and never charges the budget. |
createResponse | See the overload below for full inputs and outputs. |
Member details
measure
Preflight measurement of one input shape.
Never marks the call used and never charges the budget.
PreparedModelCall.measure(input: string | readonly ModelMessage[] | BuiltContext): ModelInputMeasurementPreflight measurement of one input shape.
Never marks the call used and never charges the budget.
| Parameter | Type | Passing | Description |
|---|---|---|---|
input | string | readonly ModelMessage[] | BuiltContext | Required | — |
Optional shapes of Parameter input: choose one shape; required fields for each branch are listed separately.
Branch 1
string
Branch 2
readonly ModelMessage[]
Branch 3
| Field | Type | Required | Description |
|---|---|---|---|
messages | ModelMessage[] | Required | Messages in final model order. |
fragmentUsage | ContextFragmentUsage[] | Required | Per-fragment accounting in the same order as the fragments were merged. |
inputChars | number | Required | Total counted characters across messages and usage. |
maxInputChars | number | Required | Budget the assembly was validated against; Infinity means unbounded. |
Returns: ModelInputMeasurement.
Return value 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. |
createResponse
PreparedModelCall.createResponse(): ModelResponseCollector<Result>No call parameters.
Returns: ModelResponseCollector<Result>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
call | { id: string; name: string; argumentChars: number; } | undefined | Optional | Read-only. Native function call that carried the selection, when the response used one. |
calls | readonly { id: string; name: string; argumentChars: number; toolId?: string; }[] | undefined | Optional | Read-only. All native calls carried by a session response; omitted by single-selection collectors. |
outputChars | number | Required | Read-only. Output characters accumulated so far, for output-budget accounting. |
push | (part: ModelPart) => void | Required | Appends one streamed part; parts arrive in provider order. |
finish | () => Result | Required | Finalizes the response. Throws when the accumulated content is not a valid selection. |
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
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);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
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);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);