Providers
Restate Harness connects to providers through the Vercel AI SDK. The AI SDK implements each provider’s API, and Restate Harness runs the agent loop, the tools, the retries, and the journaling.- Install the provider package, for example
@ai-sdk/openai,@ai-sdk/anthropic,@ai-sdk/amazon-bedrock,@ai-sdk/google,@ai-sdk/mistral, or@openrouter/ai-sdk-provider. - Create the provider’s model and wrap it with
aiModel:
OPENAI_API_KEY or ANTHROPIC_API_KEY, when the agent makes a model call, not when you create the model.
To pass an API key, a base URL, or a custom fetch explicitly, create the provider with its factory function, such as createOpenAI or createAmazonBedrock.
The OpenAI provider uses the Responses API. For a server that implements only the Chat Completions API, use createOpenAI({baseURL, apiKey}).chat(modelId).
Retries
A model call makes three attempts by default. When the attempts run out, the agent run throws aTerminalError. Pass retry to change the policy:
aiModel turns off the AI SDK’s own retries, so the retry policy controls all attempts.
When the provider throttles a call, the next attempt waits for the delay that the provider requests, for example in a retry-after header.
Some failures can’t be fixed by retrying, such as a missing API key, a feature that the provider doesn’t support, or a request that the provider rejects.
aiModel fails these calls immediately, and run throws a TerminalError. For a rejected request, the error code is the provider’s HTTP status code.
A request that is longer than the model’s context window is the exception: the run shrinks its context and sends the request once more. See Context window.
A response that the model can fix, such as an empty reply or one stopped by a content filter, goes back to the model as a note instead of being retried. A reply cut off at the reply limit fails the run instead. See Model call failures.
Reply limit
maxOutputTokens on the agent limits the tokens of each model reply, including compaction summaries that the agent’s model writes, but not the summary of a stopped or interrupted run. When it’s unset, Restate Harness leaves it out of the request, and the AI SDK provider chooses the limit:
A reply cut off at the limit fails the run, so set
maxOutputTokens when you expect long replies. See Model call failures.
Prompt caching
Prompt caching is on by default. For Anthropic and Bedrock,aiModel marks the instructions, the newest message, and the end of the previous request as cache points, so each step reads the previous step’s prefix from the cache.
Other providers ignore these markers.
Pass caching: false to send no cache points, for example for a Bedrock model without prompt caching.
Reasoning models
Configure reasoning through the provider’s options. For example, turn on adaptive thinking for Claude:reasoningEffort. For Gemini, set thinkingConfig.
Reasoning tokens count toward the reply limit: with aiModel, a reply whose output tokens, reasoning included, reach maxOutputTokens counts as cut off. If you set maxOutputTokens, leave room for the reasoning.
aiModel keeps the model’s reasoning and sends it back on later requests, across the steps of a run and the runs of a conversation.
Don’t edit the stored reasoning: providers reject reasoning that was changed.
OpenAI structured output
OpenAI structured output uses strict mode, which requires every property in the schema:- Use
.nullable()instead of.optional()in anoutputschema. - For open-ended keys, such as
z.record()or.passthrough(), passproviderOptions: {openai: {strictJsonSchema: false}}.
strictJsonSchema: false, Restate Harness still validates the final answer against the output schema.
Token usage
result.usage sums the tokens of every model call in the run, including the summary of a stopped or interrupted run, compaction summaries, sub-agents run with agentTool, and forks. It doesn’t count model calls that tools or hooks make:
When
run throws, usageOf(error) returns the totals up to the failure.
Record them only for a TerminalError: Restate retries other errors, and the replay counts the same calls again.
Model gateway
modelGateway sends model requests to another Restate handler. Use it to keep provider keys, quotas, or routing in one service:
Custom models
A model is a generator function that takes aModelRequest and returns a ModelResponse. Wrap any other client with journaledModel, which journals each call:
{type: "final", text}, {type: "tool_calls", calls}, or, for a reply cut off at the output token limit, {type: "cut_off", text, calls} with the tool calls whose arguments are complete. A cut-off reply fails the run, as it does with aiModel. Throw a TerminalError for a failure that a retry would repeat, such as an invalid API key. The run then fails instead of retrying.
When the provider rejects a request as longer than the model’s context window, throw a ContextOverflowError, so that the run shrinks its context and asks again. See Context window.
Include usage in the response, so that result.usage counts the call’s tokens.
In the Restate UI, each step’s model call appears as a journal entry named Ask model. With modelGateway, the agent’s journal shows a call to the gateway handler, and the Ask model entry is in the gateway’s invocation. Summary calls set the request’s name: Summarize stopped run, and Compact context for compaction. journaledModel names the entry after the request’s name, then its own name option, then Ask model.