Skip to content

Models

A harness does not know a provider. It takes a model adapter in its options, an object with the LlmAdapter interface. The second entry point of the package, @kerpetensh/harness-kit/ai-sdk, builds such adapters on the AI SDK:

const llm = AiSdkAdapter.fromSettings({
provider: "openrouter",
model,
apiKey,
zdr: true,
effort: "high",
maxOutputTokens: 8000,
maxRetries: 1,
});

It needs the four model packages; see Install.

AiSdkAdapter.fromSettings builds an adapter from a provider, a model id and an API key.

Field What it is
provider "anthropic", "openai", "openrouter" or "chatgpt".
model The model id as the provider writes it. An empty one throws.
apiKey The API key. An empty one throws, except for chatgpt, which takes no key.
zdr Required, true or false. Asks OpenRouter for zero data retention. The other providers do not read it.
baseUrl Optional. Sends the requests to another address than the provider’s.
effort Optional. "low", "medium", "high", "xhigh" or "max".
maxOutputTokens Optional. The output token limit of each request, a whole number over 0.
maxRetries Optional. How often the SDK tries a failed request again. 2 when left out.

The adapters read nothing from the environment:

  • The key comes from apiKey. Reading the key is the program’s work.
  • The address comes from baseUrl or the default of the provider. A variable such as ANTHROPIC_BASE_URL changes nothing.

Each provider adapter knows the usual name of its key variable, for a program that wants to read or name it:

const name = AiSdkAdapter.ADAPTERS.anthropic.keyEnv;
Provider Adapter class keyEnv Default address API it calls
anthropic AnthropicAdapter ANTHROPIC_API_KEY https://api.anthropic.com/v1 Messages
openai OpenAiAdapter OPENAI_API_KEY https://api.openai.com/v1 Responses; chat completions with a baseUrl
openrouter OpenRouterAdapter OPENROUTER_API_KEY https://openrouter.ai/api/v1 Chat completions
chatgpt ChatGptAdapter none https://api.openai.com/v1 Responses
  • It marks the instructions, the context and the newest message for the prompt cache.
  • The API needs an output token limit on every request. So an answer is never cut at a limit nobody chose:
Case Without maxOutputTokens
A model the SDK knows The SDK sends the limit of the model.
A model behind another baseUrl The limit is not known. fromSettings throws, before any request.
A model on Anthropic’s own address that the SDK does not know Its first request ends as an error that asks for the setting.

Both messages name the setting max_output_tokens, as Kerpeten’s configuration does.

With a baseUrl it calls the chat completions API of that address:

const llm = AiSdkAdapter.fromSettings({
provider: "openai",
model,
apiKey,
baseUrl: "https://models.example/v1",
zdr: false,
});

So a model server on your machine works too, such as Ollama, LM Studio or llama.cpp. For Ollama, baseUrl is http://localhost:11434/v1 and model is the name you pulled. The server does not check the key, but fromSettings needs one that is not empty.

On OpenAI’s own address, also when baseUrl is set to it:

  • A GPT-5.6 or later model gets a cache breakpoint at the end of the system message, so parallel sessions that share the instructions and the tools read them from the cache. It sends no prompt_cache_key.
  • An earlier model, or a name that is no gpt- name, sends the session’s id as prompt_cache_key on every request of the session.
  • Every tool step sends the final answer’s format; see How the adapter runs a session.

Another baseUrl gets none of these.

  • zdr: true asks for zero data retention. It is sent only on OpenRouter’s own address, not with a baseUrl.
  • It marks the prompt for the cache, also only on its own address. A tool message carries one mark, on its last tool result, so a step with several tool calls stays within Anthropic’s limit of four marks.
  • Every request of a session carries the session’s id in the x-session-id header, also with a baseUrl. OpenRouter then keeps the session on one upstream provider from the first request on, so the next requests read the cache the first one wrote.
  • It has no max effort. effort: "max" throws; the highest is xhigh.

effort goes to each provider in that provider’s own option:

Provider Option
Anthropic effort
OpenAI reasoningEffort
OpenRouter reasoning.effort

On OpenAI’s own address the SDK leaves the effort out for a model it does not know as a reasoning model.

ChatGptAdapter calls the Responses API with the bearer token of a ChatGPT plan sign-in in place of an API key.

const plan = new ChatGptAdapter({ token: accessToken });
const llm = AiSdkAdapter.fromSettings({ provider: "chatgpt", model, apiKey: "", zdr: false }, plan);
  • The kit does not sign in. The program gives a function that returns the token for the next request, refreshes it when needed and throws when there is none.
  • fromSettings takes the ChatGptAdapter as its second argument. Without it, the provider chatgpt throws.
  • Use one ChatGptAdapter for every model of the program, so that the first refusal stops them all.

The options of ChatGptAdapter:

Option What it is
token The function that gives the bearer token.
baseUrl Optional. The address of the API. https://api.openai.com/v1 when left out.
namespace Optional. The name and description of the one namespace the tools are put in. tools when left out.
modelListTimeoutMs Optional. The time limit of the model list request. 30 seconds when left out.

What it does differently from the other adapters:

  • Every request is streamed and is sent with store: false.
  • It leaves out the output token limit and the sampling settings, which plan usage does not accept. So maxOutputTokens has no effect.
  • It sends the instructions as a developer message.
  • It sends the output schema without strict mode.
  • Every request of a session carries the session’s id as prompt_cache_key and in the session-id and thread-id headers, as Codex does, so the requests of a session reach the cache it wrote.
  • Every tool step sends the final answer’s format; see How the adapter runs a session.
  • The first plan usage refusal or token failure stops the adapter. Every later request of every model it made throws the same error without reaching the network, and plan.failure holds it.
  • A refusal is a ChatGptError. Its kind is usage_limit, usage_unavailable, unauthorized or refused, and it has the code and the HTTP status when the API gave them. Its message never holds a token.

plan.listModels() lists the models of the signed-in account that are meant for display. A plan usage refusal of this request stops the adapter too; any other HTTP error fails this call only.

try {
const models = await plan.listModels();
return models.map((model) => `${model.slug} (${model.displayName})`);
} catch (error) {
if (error instanceof ChatGptError) return [`plan usage stopped the request: ${error.kind}`];
throw error;
}

Kerpeten uses this adapter for its chatgpt provider. The sign-in, the token file and the refresh are Kerpeten’s own code and are not in the kit.

fromSettings covers the four providers. For any other model object of the AI SDK, build the adapter with new:

const llm = new AiSdkAdapter({ model, provider: "openai", modelId: "my-model", maxOutputTokens: 4000 });
Field What it is
model The language model object. A model id string does not work here.
provider, modelId What llm.provider and llm.model give. provider is one of the four provider names.
maxOutputTokens, cache, effort, maxRetries Optional.
identity Optional. A function that turns the id a session makes for itself into the headers and provider options of each of its requests. fromSettings sets it from the provider adapter’s sessionIdentity.
formatOnEveryCall Optional, false when left out. Sends the final answer’s format on every tool step. fromSettings sets it from the provider adapter’s formatOnEveryCall.

This is also how a test gives a scripted model.

  • The instructions are the system message and the context is the first user message.
  • In the tool phase the model may call tools or not. Tool calls run one at a time.
  • A tool result goes back as JSON, a failed call as an error text.
  • A tool call the model’s answer cut off is not run. The model gets one error answer for it.
  • For the final answer the tools stay in the request but the model may not call them, and the answer is asked for as structured output by the schema.
  • When the provider adapter’s formatOnEveryCall says so, every tool step sends the final answer’s format too, so the final call differs from a tool step only by its new messages and its tool choice, and reads the prefix the tool steps cached. This is so for chatgpt and for openai on OpenAI’s own address. The model can still call tools on every step.
  • On those providers, the text the model ends the tool phase with is in the answer’s format. The session offers it through closingAnswer, and the harness takes it as the answer when it fits the schema; see The final answer.
  • The final answer is tried up to three times when the answer is empty, calls a tool or does not fit the schema. After the third time the outcome is invalid_output.
  • An error text is cut at 500 characters.

A program can write its own adapter for a provider or an SDK the kit has none for. LlmAdapter is part of the main entry point, which imports no model package.

Member What it is
provider The name of the provider, a string.
model The model id, a string.
start(input) Starts a session for one harness run. input holds the instructions, the context, the tools and, optionally, output, the schema of the final answer. HarnessBase fills output; an adapter may ignore it.

A session has three methods:

Method What it does
runTools(hooks, signal) Runs the tool phase. It hands each tool call to hooks.runTool, asks hooks.shouldStop after each step whether to end the phase, and reports the token counts of each model call to hooks.onUsage. It reports a call it could not run to hooks.onRefusedCall or hooks.onCutOffCall.
finalize(request, output, signal) Asks for the final answer and reads it by the zod schema output.
closingAnswer(output) The text the model ended the tool phase with, checked against output: ok: true with the value, or ok: false with the error, each with the usage of that step. undefined when the session has none to offer.

runTools and finalize give an outcome and do not throw for a model failure: ok: false comes with a kind (model_error, aborted, and for the final answer also invalid_output) and the error text.

closingAnswer is required:

  • A session that never offers a closing answer returns undefined.
  • A session that wraps another, for example to record its usage, forwards the call to the session it wraps.
  • HarnessBase refuses a session without the method before the first model call: run throws the session of the model client <provider>/<model> has no closingAnswer; a session that wraps another must forward it.

AiSdkAdapter.fromSettings takes a ProviderAdapter as its second argument, as it takes the ChatGptAdapter. Besides the model and the effort options, a provider adapter has these methods, each with a default:

Method What it gives Default
cacheMarker(settings) The provider options that mark a message for the prompt cache. undefined: no marks
sessionIdentity(sessionId, settings?) What every request of one session carries: headers and providerOptions. sessionId stays the same for all requests of the session; AiSdkAdapter always passes the settings. undefined: nothing
formatOnEveryCall(settings) Whether every tool step sends the final answer’s format. Only for an API that keys its cache on the output format and lets the model call tools while a format is set, as the OpenAI Responses API does. false

The smallest adapter is the scripted one on the Testing page.

sdkRoundTrip() runs a scripted model through a harness with one tool, without the network: a tool call, a stop and a final answer. It throws when one of the parts did not work.

import { sdkRoundTrip } from "@kerpetensh/harness-kit/ai-sdk";
await sdkRoundTrip();

A program can run it as a check of its installation. Kerpeten does so in its self-check command.