<!-- Markdown twin of https://eugene1g.github.io/effect-handbook/systems/ai-language-models · The Effect 4 Handbook (effect@4.1.4, audited 2026-11-19) · Index: https://eugene1g.github.io/effect-handbook/llms.txt · Modules: https://eugene1g.github.io/effect-handbook/effect-4-modules.md · Agent guide: https://eugene1g.github.io/effect-handbook/reference/agent-guide.md -->

# AI & Language Models

**Contents:** [LanguageModel](#languagemodel) · [Chat](#chat) · [Tool](#tool) · [Toolkit](#toolkit) · [Prompt](#prompt) · [Response](#response) · [EmbeddingModel](#embeddingmodel) · [Decision](#decision) · [DecisionModel](#decisionmodel) · [Model](#model) · [Tokenizer](#tokenizer) · [IdGenerator](#idgenerator) · [Telemetry](#telemetry) · [AiError](#aierror) · [ResponseIdTracker](#responseidtracker) · [AnthropicStructuredOutput](#anthropicstructuredoutput) · [OpenAiStructuredOutput](#openaistructuredoutput) · [WorkloadIdentity](#workloadidentity) · [AgUiSchema](#aguischema) · [AgUi](#agui) · [AgUiServer](#aguiserver) · [Production rules for model calls](#production-rules-for-model-calls) · [Provider packages](#provider-packages)

`effect/ai` provides a provider-agnostic AI toolkit. Business logic depends on `LanguageModel.LanguageModel` from context; a concrete provider (OpenAI, Anthropic, Amazon Bedrock, OpenRouter, or any OpenAI-compatible endpoint) is injected as a `Layer`. Schemas validate structured outputs and tool parameters, streaming is a `Stream`, errors are typed, and all calls are traced. Swapping providers requires changing a Layer, not application code. For fixed-answer judgments (a label, a rating on a scale, or a probability) there is a second, narrower service, [`DecisionModel`](#decisionmodel), with its own providers.

Model Context Protocol servers built from the same tools, and `McpClient` for consuming another server's tools as a `Toolkit`, are on [MCP Servers](mcp.md).

> **Official companions:** The release-matched [AI examples](https://github.com/Effect-TS/effect/tree/effect%404.1.4/ai-docs/src/71_ai) cover language-model calls, tools, and stateful chat. The broader [AI documentation source tree](https://github.com/Effect-TS/effect/tree/effect%404.1.4/ai-docs/src) and [`LLMS.md`](https://github.com/Effect-TS/effect/blob/effect%404.1.4/LLMS.md) are the official executable corpus and coding-agent entry point.

> **Note:** Every example below assumes a provider client Layer built from `Config`. Providers need an `HttpClient` — you choose which one (here `FetchHttpClient`):

```ts
import { Config, Layer } from "effect"
import { OpenAiClient } from "@effect/ai-openai"
import { FetchHttpClient } from "effect/http"

// Reads OPENAI_API_KEY from your ConfigProvider; the key is Redacted, so it
// never leaks into logs. Provide an HttpClient for the provider to use.
const OpenAiClientLayer = OpenAiClient.layerConfig({
  apiKey: Config.Redacted("OPENAI_API_KEY")
}).pipe(Layer.provide(FetchHttpClient.layer))
```

## LanguageModel

`effect/ai/LanguageModel` — unstable

A provider-agnostic service for generating text, schema-validated objects, and streaming with first-class tool calling. Write against `LanguageModel.LanguageModel`; supply a provider Layer via `Effect.provide`.

Three verbs: `generateText` returns a rich response (text, tool calls/results, finish reason, token usage). `generateObject` asks for JSON and decodes it through your `Schema` — `response.value` is typed and validated; bad output becomes a typed `AiError`. `streamText` returns a `Stream` of response parts. Static functions (`LanguageModel.generateText(...)`) read the model from context; identical methods exist on the yielded service value.

```ts
import { Effect, Schema } from "effect"
import { LanguageModel } from "effect/ai"
import { OpenAiLanguageModel } from "@effect/ai-openai"

// 1) Plain text. `generateText` returns text + usage + finishReason.
//    Here: draft a concise performance-review summary from a manager's notes.
const draftReviewSummary = Effect.fn("draftReviewSummary")(function*(notes: string) {
  const response = yield* LanguageModel.generateText({
    prompt: `Summarize this engineer's annual performance in two sentences, ` +
      `neutral tone, suitable for a review packet:\n${notes}`
  })
  yield* Effect.log(`out tokens: ${response.usage.outputTokens.total}`)
  return response.text
})

// 2) Schema-validated object. The model's JSON is DECODED through this schema,
//    so `value` is a real, validated `RaiseRecommendation` — or a typed AiError.
const RaiseRecommendation = Schema.Struct({
  employeeId: Schema.String,
  rating: Schema.Literals(["below", "meets", "exceeds"]),
  proposedIncreasePct: Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 1 })),
  rationale: Schema.String
})

const recommendRaise = Effect.fn("recommendRaise")(function*(reviewNotes: string) {
  const response = yield* LanguageModel.generateObject({
    objectName: "raise_recommendation",
    prompt: `From these review notes, propose a merit increase within band:\n${reviewNotes}`,
    schema: RaiseRecommendation
  })
  return response.value // typeof RaiseRecommendation.Type, fully decoded
})

// Provide a concrete provider at the edge. `.model(...)` returns a Model
// (a Layer) carrying the OpenAI client requirement — swap this one line to
// switch providers, no business-logic changes.
const program = draftReviewSummary("…").pipe(
  Effect.provide(OpenAiLanguageModel.model("gpt-5.2"))
)
```

**An empty reply is a failure unless you allow it.** By default `generateObject` fails with a `StructuredOutputError` when the model returns no text. Pass `allowEmpty: true` when "nothing to extract" is a legitimate answer: empty or whitespace-only text then yields `Option.none()`, and valid output `Option.some(value)`; nonempty text that does not decode still fails. Only the literal `true` types `value` as `Option<A>` (`LanguageModel.ExtractObjectValue` computes it); a widened `boolean` types it as `A | Option<A>`. `Chat.generateObject` accepts the same option.

```ts
import { Effect, Option, Schema } from "effect"
import { LanguageModel } from "effect/ai"

const Escalation = Schema.Struct({ employeeId: Schema.String, reason: Schema.String })

// Most review notes contain no escalation; an empty reply means "none", not an error.
const findEscalation = Effect.fn("findEscalation")(function*(notes: string) {
  const response = yield* LanguageModel.generateObject({
    objectName: "escalation",
    prompt: `If these notes require an HR escalation, describe it; otherwise reply with nothing:\n${notes}`,
    schema: Escalation,
    allowEmpty: true
  })
  return Option.map(response.value, (escalation) => escalation.reason) // Option<string>
})
```

Streaming yields typed parts; filter for the deltas you need:

```ts
import { Stream } from "effect"
import { LanguageModel, type Response } from "effect/ai"
import { OpenAiLanguageModel } from "@effect/ai-openai"

const summaryTokens = LanguageModel.streamText({
  prompt: "Draft talking points for a promotion case as a bulleted list."
}).pipe(
  // Each chunk is a Response part: text-start, text-delta, finish, tool-call…
  Stream.filter((part): part is Response.TextDeltaPart => part.type === "text-delta"),
  Stream.map((part) => part.delta),
  Stream.provide(OpenAiLanguageModel.model("gpt-5.2"))
)
```

> **Tip:** Because a model is just a Layer of requirements, you can wrap several in an `ExecutionPlan` — try a cheap model up to N times, then fall back to a stronger one — and apply it with `Effect.withExecutionPlan`. Use `plan.captureRequirements` to fold both providers' client requirements into your service Layer.

```ts
import { Effect, ExecutionPlan } from "effect"
import { LanguageModel } from "effect/ai"
import { OpenAiLanguageModel } from "@effect/ai-openai"
import { AnthropicLanguageModel } from "@effect/ai-anthropic"

const ReviewPlan = ExecutionPlan.make(
  { provide: OpenAiLanguageModel.model("gpt-5.2"), attempts: 3 },
  { provide: AnthropicLanguageModel.model("claude-opus-4-6"), attempts: 2 }
)

const draft = LanguageModel.generateText({ prompt: "Summarize this review cycle." })
  .pipe(Effect.withExecutionPlan(ReviewPlan))
```

### Tool-call resolution: concurrency and manual dispatch

When the `toolkit` you pass carries handlers, `generateText`, `generateObject`, and `streamText` resolve the model's tool calls **during the call**: parameters are decoded, handlers run, and the results come back in `response.toolResults`. Two options on every generation call govern that step.

| Option | Default | Rule |
| --- | --- | --- |
| `concurrency` | `"unbounded"` | **Set a number whenever a handler touches a rate-limited or stateful dependency**, because one model turn may request many tool calls and they all start at once by default. |
| `disableToolCallResolution` | `false` | **Pass `true` when the application must run its own gates (authorization, approval, idempotency claim) before any handler executes.** The tools are still advertised to the model; no handler runs. |

With `disableToolCallResolution: true` under the default strict validation, user-tool `params` stay in their **JSON encoding**, typed as `Schema.Json` (a malformed call still fails with `InvalidOutputError`). Decode with `Schema.toCodecJson(tool.parametersSchema)` before passing arguments to typed authorization or policy code. No handler result is produced, and handler errors and handler services drop out of the call's error and requirement types. Dispatch a call yourself through the toolkit's `handle(name, jsonParams, toolCallId?, parseOptions?)`, which decodes the JSON parameters, runs the handler, and returns a `Stream` of `{ result, encodedResult, isFailure, failureOrigin, preliminary }` values: preliminary results first, then the final one. Pass the original JSON to `handle`, not the decoded domain value: a `Schema.Date` parameter, for example, travels as an ISO string, and an omitted optional parameter you construct yourself is `null` rather than `undefined`. The optional `SchemaAST.ParseOptions` tune the parameter decode; `{ onExcessProperty: "error" }` rejects arguments the schema does not declare. With `failureMode: "error"` a parameter failure fails the returned Effect and a handler failure fails the stream instead of producing a final value.

For an application-owned validation loop, combine `disableToolCallResolution: true` with `toolCallValidation: "deferred"`. Model-generated calls then carry `unknown` parameters and may name undeclared tools, even without a toolkit. Validate the name and decode/normalize the arguments before authorization or dispatch. Strict validation remains the default; provider-executed calls remain validated in deferred mode too. Anthropic, OpenAI, and OpenAI-compatible providers support this option. Deferring validation is not permission to execute arbitrary names or arguments.

Every failure is tagged with the phase that produced it, a `Tool.FailureOrigin`: `"parameters"` (arguments did not decode), `"handler"` (the handler failed), or `"result"` (the handler's output failed to validate or encode). A returned failure carries it as `failureOrigin`; a raised one carries it as the `Toolkit.FailureOrigin` annotation on its `Cause`, read with `Context.get(Cause.annotations(cause), Toolkit.FailureOrigin)`. Use it to tell "the model sent bad arguments" from "the tool broke" without parsing messages.

```ts
import { Effect, Schema, Stream } from "effect"
import { LanguageModel, Tool, Toolkit } from "effect/ai"

class RaiseGateDenied extends Schema.TaggedError<RaiseGateDenied>()("RaiseGateDenied", {
  toolCallId: Schema.String,
  reason: Schema.String
}) {}

const SubmitRaise = Tool.make("SubmitRaise", {
  description: "Submit a merit increase for HRBP approval",
  parameters: Schema.Struct({ employeeId: Schema.String, increasePct: Schema.Finite }),
  success: Schema.Struct({ requestId: Schema.String })
})
const RaiseToolkit = Toolkit.make(SubmitRaise)

// Application policy: actor, tenant, band limits, idempotency claim.
declare const authorizeRaise: (
  toolCallId: string,
  params: { readonly employeeId: string; readonly increasePct: number }
) => Effect.Effect<void, RaiseGateDenied>

const proposeAndSubmit = Effect.fn("proposeAndSubmit")(function*(notes: string) {
  const toolkit = yield* RaiseToolkit // handlers come from RaiseToolkit.toLayer(...)
  const response = yield* LanguageModel.generateText({
    prompt: `Propose a merit increase from these notes:\n${notes}`,
    toolkit,
    disableToolCallResolution: true // the model proposes; nothing executes yet
  })

  const requestIds: Array<string> = []
  for (const call of response.toolCalls) {
    const params = yield* Schema.decodeUnknownEffect(
      Schema.toCodecJson(SubmitRaise.parametersSchema)
    )(call.params)
    yield* authorizeRaise(call.id, params)
    // Run `handle` and drain its stream in the same Effect: the handler is a
    // child fiber of this call, so interrupting the request interrupts it too.
    const results = yield* toolkit.handle(call.name, call.params, call.id)
    const final = yield* Stream.runLast(results)
    // `result` is the success type, the declared failure type, or a
    // Tool.ExecutionFailure; `isFailure` is a plain boolean, so narrow structurally.
    if (final._tag === "Some" && "requestId" in final.value.result) {
      requestIds.push(final.value.result.requestId)
    }
  }
  return requestIds
})
```

> **Note:** When a response finishes with an incomplete reason (anything other than `"stop"`, `"tool-calls"`, or `"pause"` — for example `"length"`), automatic resolution does not start handlers. Each executable tool call instead receives a synthesized failed result of type `"execution-interrupted"` so the history stays well-formed for the next provider request.

> **Note:** Calls to a [client-executed tool](#client-executed-tools) are never resolved, even with resolution enabled: they appear in `response.toolCalls` without a result, and `Chat.streamText` stops its step loop there so the application can answer them.

### Service types are branded

`LanguageModel.LanguageModel`, `EmbeddingModel.EmbeddingModel`, `Chat.Chat`, and `Reactivity.Reactivity` each name **both** the Context key and the service interface; the older `LanguageModel.Service` / `Chat.Service` / `EmbeddingModel.Service` type aliases no longer exist. Each interface carries a `[TypeId]` brand. **Build implementations with the module's constructor** (`LanguageModel.make`, `EmbeddingModel.make`, `Chat.empty` / `Chat.fromPrompt`, `Reactivity.make`), because the constructor adds the brand; an object literal written by hand must include `[LanguageModel.TypeId]: LanguageModel.TypeId` (and the equivalent for the other modules) to type-check.

**Reach for it when** you need any LLM call — text, validated structured data, streaming, or tool use — without coupling to a vendor SDK. This is the default entry point.

## Chat

`effect/ai/Chat` — unstable

A stateful conversation on top of `LanguageModel`. Owns a mutable history `Ref`; each turn automatically includes prior messages, and an agentic loop automatically appends tool results before the next turn.

Create with `Chat.empty`, `Chat.fromPrompt` (seed a system message), or `Chat.fromJson`/`fromExport` (rehydrate). Call `session.generateText({ prompt })` per turn; history is threaded automatically. Inspect or persist via `session.history` (a `Ref`) and `session.exportJson`. `Chat.makePersisted` / `Chat.layerPersisted` support durable sessions.

```ts
import { Effect, Ref } from "effect"
import { Chat, Prompt } from "effect/ai"
import { OpenAiLanguageModel } from "@effect/ai-openai"

// An HRBP assistant: a multi-turn helper for an HR business partner.
const hrbpSession = Effect.gen(function*() {
  // Seed with a system message; history is maintained automatically.
  const session = yield* Chat.fromPrompt(
    Prompt.empty.pipe(
      Prompt.setSystem("You are an HRBP assistant. Stay within comp policy and be concise.")
    )
  )

  const first = yield* session.generateText({
    prompt: "What's a typical merit increase for a strong-performing L4?"
  })
  // The next turn sees the previous question AND answer — no manual context.
  const second = yield* session.generateText({
    prompt: "And if their salary is already at band midpoint?"
  })

  const history = yield* Ref.get(session.history)
  yield* Effect.log(`history has ${history.content.length} messages`)

  // Persist the whole conversation as JSON and rehydrate later.
  const json = yield* session.exportJson
  return { first: first.text, second: second.text, json }
}).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-5.2")))
```

For an agent, loop until the model stops calling tools — `Chat` folds each tool result back into history between turns. **Give the loop a turn limit**: a model that keeps requesting tools would otherwise spend tokens until something external stops it.

```ts
import { Effect, Schema } from "effect"
import { Chat, Tool, Toolkit } from "effect/ai"

class AgentTurnLimit extends Schema.TaggedError<AgentTurnLimit>()("AgentTurnLimit", {
  turns: Schema.Int
}) {}

const runHrbpAgent = <Tools extends Record<string, Tool.Any>>(
  question: string,
  tools: Toolkit.Toolkit<Tools>,
  maxTurns = 6
) =>
  Effect.gen(function*() {
    const session = yield* Chat.fromPrompt([
      { role: "system", content: "Use tools to ground every comp answer in real band data." },
      { role: "user", content: question }
    ])
    for (let turn = 1; turn <= maxTurns; turn++) {
      const response = yield* session.generateText({ prompt: [], toolkit: tools, concurrency: 4 })
      // Tool calls were executed and their results added to history for us.
      if (response.toolCalls.length === 0) return response.text // final answer
    }
    return yield* new AgentTurnLimit({ turns: maxTurns })
  })
```

**Streaming multi-round turns.** `session.streamText` calls the model once by default. Its options (`Chat.StreamTextOptions`) add `maxSteps`: while a round ends with `finish` reason `"tool-calls"` and every call in it got a final result, the chat calls the model again with the updated history, up to `maxSteps` rounds in one stream. The loop also stops at a round that needs no tools, an incomplete finish, or a call left unanswered (a [client-executed tool](#client-executed-tools) or an approval request). Each round ends with its own `finish` part, so sum usage over every `finish` you see, and history records each round in order, including the partial output of an interrupted one.

```ts
import { Effect, Stream } from "effect"
import { Chat, type Response, type Tool, type Toolkit } from "effect/ai"

declare const tools: Toolkit.Toolkit<Record<string, Tool.Any>>

const streamAgentAnswer = (question: string) =>
  Stream.unwrap(Effect.map(Chat.empty, (session) =>
    session.streamText({ prompt: question, toolkit: tools, concurrency: 4, maxSteps: 5 }).pipe(
      Stream.filter((part): part is Response.TextDeltaPart => part.type === "text-delta"),
      Stream.map((part) => part.delta)
    )))
```

**History rules.**

- **`Chat` is in-process session state, not a store.** Its history `Ref` has no tenant, version, or concurrency control; wrap it, or use `Chat.makePersisted({ storeId })` / `Chat.layerPersisted` over a durable `BackingPersistence` layer (see [Persistence](../tooling/persistence.md)), when several requests or processes can touch one conversation. A stored history that no longer decodes fails with a typed `PersistenceError`, not a defect.
- **Persisted chats depend on the backing key layout.** Persistence stores are isolated under `<storeId>:<key>` keys. Histories that `Chat.layerPersisted` wrote on a `KeyValueStore` backing (`Persistence.layerKvs` / `layerBackingKvs`) under the earlier `<storeId><key>` layout are **not read back** and are not migrated or removed automatically; on Redis only store ids containing `%` or `:` are affected. Copy old keys to the new layout before upgrading if chat history must survive — see [Persistence](../tooling/persistence.md#persistence).
- **Exported history is a persisted format.** `exportJson` keeps text parts, provider options, and files generated by the model when a response is folded back into the prompt, so a rehydrated session replays the same context. `Prompt.FilePart` bytes and URLs are encoded as tagged `{ _tag: "Uint8Array", base64 }` and `{ _tag: "URL", href }` objects that decode back to their original types. Tool results inside that history hold the JSON encoding of the schema chosen by their `isFailure` flag (see [Tool](#tool)), so a `Schema.Date` result is stored as an ISO string; review stored histories when a tool's success or failure schema changes.
- **Completed approvals stay in the response.** When a non-streaming turn resolves a previously approved tool call, the finished result is retained in that response, so `Chat` records it and later turns do not execute the approved tool again.
- **Keep incomplete turns out of later prompts.** If a turn fails, times out, or is interrupted mid-stream, persist that status and rebuild future context only from turns that reached a terminal `finish` part.

**Reach for it when** building a chatbot or agent that needs memory across turns, or needs save/restore of conversation state. For one-shot calls, plain `LanguageModel` is sufficient.

## Tool

`effect/ai/Tool` — unstable

A single typed function the model can call. Bundles a name, a description (shown to the model), a `parameters` `Schema` the model fills in, and a `success` `Schema` for the handler's result. Parameters are validated on the way in; results on the way out. Both cross the boundary through the schema's **JSON codec** (`Schema.toCodecJson`): a `Schema.Date` parameter accepts the ISO string a model or MCP client sends, and a `Schema.Date` or `Schema.BigInt` result reaches the model, MCP clients, and `Chat.exportJson` as JSON. Provider-executed tools keep the provider's own payload.

`Tool.make(name, { … })` defines user tools (handler required). `Tool.providerDefined` wraps server-side provider tools (web search, code interpreter — no handler needed). `failureMode: "error"` (default) routes handler failures to the effect's error channel; `"return"` feeds the error back to the model as a tool result. Annotate parameters with `.annotate({ description })` for better model guidance.

`toModelOutput: (result) => parts` sends text and file parts (`Prompt.ToolModelOutputPart`, for example a rendered chart image) to the model in place of the encoded result, while the application still receives the typed result; Anthropic and OpenAI Responses send native content blocks, and OpenAI-compatible APIs and OpenRouter send text with a placeholder per file (`Prompt.toolModelOutputToText`). `Tool.make` and `Tool.dynamic` accept it. A tool whose parameters do not produce a JSON Schema object — `Schema.Struct({})`, say — fails with an `UnsupportedSchemaError` that names the tool; use `Tool.EmptyParams` instead.

Call `.setNeedsApproval(true)` for a sensitive tool, or pass a function of `(params, { toolCallId, messages })` for dynamic approval. Toolkit handlers receive their own second context argument with the optional `toolCallId` and a `preliminary(result)` Effect for streaming progress; retain that ID when correlating approvals, audit records, and results.

```ts
import { Effect, Schema } from "effect"
import { Tool } from "effect/ai"

const EmployeeId = Schema.String.pipe(Schema.brand("EmployeeId"))

// A tool that looks up an employee's CompBand. Name, description, an input
// schema the model fills, an output schema for the handler result. Per-parameter
// descriptions sharpen model behavior.
const LookupCompBand = Tool.make("LookupCompBand", {
  description: "Look up the salary band (min/mid/max) for an employee's level",
  parameters: Schema.Struct({
    employeeId: EmployeeId.annotate({ description: "e.g. 'emp-4821'" }),
    level: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10 })).pipe(
      Schema.withDecodingDefault(Effect.succeed(4))
    ).annotate({ description: "Job level; defaults to 4 if unknown" })
  }),
  success: Schema.Struct({
    level: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10 })),
    min: Schema.Finite,
    mid: Schema.Finite,
    max: Schema.Finite
  }),
  // "error" (default): handler failures go to the effect error channel.
  // "return": failures are returned to the model as a tool result instead.
  failureMode: "error"
})
```

### Failure results and their wire format

A tool call ends in one of four shapes, and the encoded form of each is a **persisted contract** once it lands in a `Chat` history or your own store.

| Outcome | `isFailure` | Value in `result` | Encoded with |
| --- | --- | --- | --- |
| Handler succeeded | `false` | the `success` type | JSON codec of the `success` schema |
| Handler failed with the declared `failure` type (`failureMode: "return"`) | `true` | the `failure` type | JSON codec of `Tool.failureResultSchema(tool)` |
| Framework or handler produced an `AiError` (`failureMode: "return"`) | `true` | `AiError.AiError` | JSON codec of `Tool.failureResultSchema(tool)` |
| Call was denied by an approval response, or never ran because the response finished incomplete | `true` | `Tool.ExecutionFailure` — `{ type: "execution-denied" \| "execution-interrupted", reason }` | JSON codec of `Tool.failureResultSchema(tool)` |

- **Declare `failure` when the model should see a domain error.** `Tool.make(name, { failure: BandNotFound, failureMode: "return" })` types the handler's error channel; `failureMode: "error"` keeps the same failure in the Effect error channel instead.
- **`Tool.failureResultSchema(tool)` is `Schema.Union([AiError.AiError, tool.failureSchema, Tool.ExecutionFailure])`.** Use it, not the bare `failure` schema, when you decode stored failed results.
- **The codec is selected by `isFailure`, never guessed from the value.** A tool with `success: Schema.Number` and `failure: Schema.NumberFromString` stores a failed `404` as `"404"`.
- **`Tool.FailureResult` and `Tool.Result` include `Tool.ExecutionFailure` in both failure modes**, so an exhaustive narrowing of a failed result has one more case. (`Response.ToolResultPart(...)` is typed as a `Schema.Codec` rather than a `Schema.decodeTo`; this only matters if you wrote that type out.)
- **`Schema.Void` encodes to `null`.** A tool without a `success` schema (or with `Schema.Void`) has `encodedResult: null`, not `undefined`.
- **Parameter validation follows `failureMode` too.** A call whose arguments do not decode produces `ToolParameterValidationError` (`toolName` and `description` only — the rejected arguments are no longer attached as `toolParams`). Under `"error"` the generation fails with that `AiError`; under `"return"` the error goes back to the model as a failed tool result so it can correct the call. Either way the handler does not run.

> **Note:** `Tool.dynamic(name, { parameters })` accepts a Schema **or** a raw JSON Schema for tools discovered at runtime (for example from an MCP server). After `tool.setParameters(schema)` the replacement schema is what gets advertised to the model. A tool annotated `.annotate(Tool.Strict, true)` needs an Effect Schema to be served over MCP: `McpServer.toolkit` / `registerToolkit` dies at registration on a strict dynamic tool whose parameters are raw JSON Schema, because the server could not enforce strictness on it. Annotate intent with `Tool.Title`, `Tool.Readonly`, `Tool.Destructive`, `Tool.Idempotent`, and `Tool.OpenWorld`; `McpServer` forwards them as MCP tool hints.

> **Migration:** A `Tool.dynamic` tool without a `success` schema must return a JSON value, even when `toModelOutput` replaces what the model sees; returning a `Date` or another non-JSON value now fails with `ToolResultEncodingError`. Declare a `success` schema (for example `Schema.Date`), return JSON, or declare `success: Schema.Void` for a tool that returns `undefined`. Provider-defined tools keep their provider-defined type through `setSuccess`, `setFailure`, `setParameters`, `setNeedsApproval`, `addDependency`, `annotate`, and `annotateMerge`.

`Tool.unsafeSecureJsonParse(text)` is the JSON parser for untrusted tool text: it throws a `SyntaxError` for `__proto__` keys and `constructor.prototype` shapes, including spellings hidden behind `\u` escapes.

### Client-executed tools

Some tools run in the application that sent the request — a browser widget, a desktop host — not on the server. Declare them with `execution: "client"` on `Tool.make` or `Tool.dynamic`. The model is offered the tool and its calls are emitted, but `LanguageModel` never resolves them and the tool needs no handler in `toLayer` (calling it through `toolkit.handle` dies). The application answers each call with a tool-result message in its next request. `Tool.isClientExecuted(tool)` tests the mode and narrows the tool's type; `Tool.Execution` is `"server" | "client"`, recorded as the tool's last type parameter so every combinator keeps it.

```ts
import { Effect, Schema } from "effect"
import { Tool, Toolkit } from "effect/ai"

// Runs in the HR partner's browser: shows a confirmation dialog and reports the choice.
const ConfirmInUi = Tool.make("ConfirmInUi", {
  description: "Ask the HR partner to confirm an action in their browser",
  parameters: Schema.Struct({ message: Schema.String }),
  success: Schema.Struct({ confirmed: Schema.Boolean }),
  execution: "client"
})

const LookupBand = Tool.make("LookupBand", {
  parameters: Schema.Struct({ level: Schema.Int }),
  success: Schema.Struct({ mid: Schema.Finite })
})

const Tools = Toolkit.make(ConfirmInUi, LookupBand)

// Only the server-executed tool needs a handler.
const ToolsLayer = Tools.toLayer({
  LookupBand: ({ level }) => Effect.succeed({ mid: 150_000 + level * 10_000 })
})
```

**Reach for it when** the model needs to fetch data or call an API mid-generation. Define the contract here; group and implement with `Toolkit`.

## Toolkit

`effect/ai/Toolkit` — unstable

A typed bundle of tools plus their handler implementations. `Toolkit.make(...tools)` groups any number of `Tool`s; `toolkit.toLayer(...)` produces a `Layer` satisfying every handler. The framework decodes parameters, invokes the right handler, validates the result, and feeds it back to the model automatically.

Handlers are plain Effects and can yield other services. `Toolkit.merge` combines toolkits. A handler is interrupted when its result stream closes, including early termination, and the stream waits for that interruption to finish: a defect raised by the handler's interrupt finalizers fails the stream instead of being discarded. Preliminary results are buffered without limit unless you provide `Toolkit.PreliminaryBuffer` (a number or `"unbounded"`). Under `failureMode: "error"`, a failing handler keeps every failure and its annotations in the cause.

**Tool arguments stay out of traces by default.** `handle` annotates the current span with `gen_ai.tool.name` and, when a call id is given, `gen_ai.tool.call.id`. Provide `Layer.succeed(Toolkit.RecordToolArguments, true)` to also record the arguments as JSON in `gen_ai.tool.call.arguments`; arguments carry user data, so enable it only where traces may hold it. `McpServer` reads the same setting for `tools/call` spans.

> **Migration:** Tool spans no longer carry the `tool` and `parameters` attributes. Rename dashboards and queries to `gen_ai.tool.name`, and opt in with `Toolkit.RecordToolArguments` if you relied on recorded parameters.

Decoding tool calls needs the tools' parameter-schema services: `Response.AllParts`, `Response.Part`, and `Response.StreamPart` require them (exposed as `Tool.ParametersDecodingServices<T>`), including for toolkits with handlers. Called without a toolkit, they decode tool-call and tool-result parts for any tool name with `unknown` parameters and results. Provider-defined tools (e.g. `OpenAiTool.WebSearch`) can sit alongside user tools and run server-side, so they require no handler in `toLayer`.

```ts
import { Context, Effect, Layer, Schema } from "effect"
import { AiError, LanguageModel, Tool, Toolkit } from "effect/ai"
import { OpenAiLanguageModel } from "@effect/ai-openai"

// Kept self-contained here; a real module would reuse the Tool declared above.
const LookupCompBand = Tool.make("LookupCompBand", {
  description: "Look up the salary band for a job level",
  parameters: Schema.Struct({ level: Schema.Int }),
  success: Schema.Struct({
    level: Schema.Int,
    min: Schema.Finite,
    mid: Schema.Finite,
    max: Schema.Finite
  })
})

const CompToolkit = Toolkit.make(LookupCompBand /*, GetEmployee, … */)

// Implement every handler. Use the effectful toLayer factory only when
// constructing the handlers themselves needs services.
const CompToolkitLayer = CompToolkit.toLayer({
  LookupCompBand: Effect.fn("LookupCompBand")(function*({ level }) {
    // In real code, yield* an Hris service and read the band table.
    return { level, min: 150_000, mid: 185_000, max: 220_000 }
  })
})

// Wire it into a model call. Set toolChoice: "required" to force a tool call.
class CompAssistant extends Context.Service<CompAssistant, {
  answer: (q: string) => Effect.Effect<string, AiError.AiError>
}>()("app/CompAssistant") {
  static layer = Layer.effect(
    CompAssistant,
    Effect.gen(function*() {
      const toolkit = yield* CompToolkit // resolves handlers from context
      // Capture OpenAiClient while constructing this service, so callers of
      // answer only see the declared AiError and no provider requirement.
      const model = yield* OpenAiLanguageModel.model("gpt-5.2").captureRequirements
      return CompAssistant.of({
        answer: Effect.fn("answer")(function*(question: string) {
          const response = yield* LanguageModel.generateText({
            prompt: question,
            toolkit,
            toolChoice: "auto"
          })
          // Inspect what the model did: response.toolCalls / response.toolResults
          return response.text
        }, Effect.provide(model))
      })
    })
  ).pipe(Layer.provide(CompToolkitLayer))
}
```

Yielding a toolkit (`yield* CompToolkit`) resolves its handlers from context and returns a `Toolkit.WithHandler` with two members: `tools` and `handle(name, params, toolCallId?)`. `handle` takes the parameters' **JSON encoding**, typed `Schema.Json` — what a provider or a stored tool call actually holds, with `null` rather than `undefined` for an omitted optional parameter — decodes it with the parameter schema's JSON codec, and returns an Effect of a result `Stream`. That is the entry point for [manual dispatch](#tool-call-resolution-concurrency-and-manual-dispatch) and for tests that exercise one handler without a model.

**Reach for it when** exposing one or more tools to a model. Define tools with `Tool`, group here, implement handlers with `toLayer`, and pass the toolkit to `generateText` or `Chat`.

## Prompt

`effect/ai/Prompt` — unstable

The provider-neutral representation of conversation input: an ordered list of `Message`s (system/user/assistant), each composed of typed `Part`s (text, file, reasoning, tool-call, tool-result).

`RawInput` means you can pass a plain string, an array of message objects, or a real `Prompt` — `Prompt.make` normalizes all three. Combinators: `Prompt.empty`, `concat` (append), `setSystem` (replace system message), `fromMessages`, and `fromResponseParts` (convert a model response into prompt history).

```ts
import { Prompt } from "effect/ai"

// A string is the simplest prompt (becomes a single user message).
const p0 = Prompt.make("Summarize this employee's review in one paragraph.")

// Build structured, multi-message prompts and compose them.
const system = Prompt.make([{ role: "system", content: "You are a compensation analyst." }])
const user = Prompt.make([{ role: "user", content: "Is this raise within band?" }])

const combined = Prompt.concat(system, user)
// Replace whatever system message exists with a new one.
const retargeted = Prompt.setSystem(combined, "You are a strict comp-policy reviewer.")
```

**Reach for it when** assembling prompts programmatically — injecting a system persona, stitching few-shot examples, or passing multi-part (text + file) input. For trivial calls, pass a string directly.

## Response

`effect/ai/Response` — unstable

The typed vocabulary of model output. A non-streaming call returns a `GenerateTextResponse` whose `content` is an array of `Part`s; streaming yields a sequence of `StreamPart`s. Convenience getters avoid manual part parsing.

Output is a tagged stream of parts: `text-start` / `text-delta` / `text-end`, `reasoning-*`, `tool-call`, `tool-result`, and a terminal `finish` carrying finish reason and token `Usage`. Accessors: `.text`, `.reasoningText`, `.toolCalls`, `.toolResults`, `.finishReason`, and `.usage` (with nested `inputTokens`/`outputTokens` breakdowns). A tool-result part's `encodedResult` (and the `result` of a prompt tool-result part) holds the JSON encoding for locally handled tools and the provider's own payload for provider-executed ones.

```ts
import { Effect } from "effect"
import { LanguageModel, type Response } from "effect/ai"

const inspect = Effect.gen(function*() {
  const res = yield* LanguageModel.generateText({ prompt: "Draft a one-line raise rationale." })

  res.text             // concatenated text parts
  res.finishReason     // "stop" | "length" | "tool-calls" | …
  res.usage.inputTokens.total
  res.usage.outputTokens.total

  // When streaming, branch on the part tag:
  const onPart = (part: Response.StreamPart<{}>) => {
    if (part.type === "text-delta") return part.delta
    if (part.type === "finish") return `done: ${part.reason}`
    return ""
  }
  return onPart
})
```

**Reach for it when** you need more than the final string — token accounting, finish reasons, reasoning traces, or precise streaming part handling.

## EmbeddingModel

`effect/ai/EmbeddingModel` — unstable

A provider-agnostic service for turning text into vectors. `embed(input)` returns one embedding; `embedMany(inputs)` returns a batch with usage metadata. The single-input path is backed by a `RequestResolver`, so concurrent `embed` calls are automatically batched into one provider request.

Write against `EmbeddingModel.EmbeddingModel`; provide a provider Layer (e.g. `OpenAiEmbeddingModel.model("text-embedding-3-small", { dimensions: 1536 })`). Results preserve input order; `embedMany([])` short-circuits without calling the provider. The configured vector size is available via the `EmbeddingModel.Dimensions` service. Concurrent `embed` calls share one `embedMany` call only when the optional `batchKey(context)` passed to `EmbeddingModel.make` returns equal keys; the batch runs with the **first caller's context**, so context that `batchKey` does not return (per-call `FetchHttpClient.RequestInit`, say) is not isolated. The OpenAI providers key batches by configuration overrides and client transforms.

```ts
import { Effect } from "effect"
import { EmbeddingModel } from "effect/ai"
import { OpenAiEmbeddingModel } from "@effect/ai-openai"

// Embed job descriptions so you can match roles, detect duplicate reqs, or
// power semantic search over the org's open positions.
const embedJobDescriptions = Effect.gen(function*() {
  const model = yield* EmbeddingModel.EmbeddingModel

  // Auto-batched: these two run concurrently but hit the provider once.
  const [backend, frontend] = yield* Effect.all(
    [
      model.embed("Senior Backend Engineer — distributed systems, Go, on-call"),
      model.embed("Senior Frontend Engineer — React, design systems, a11y")
    ],
    { concurrency: "unbounded" }
  )

  // Or batch explicitly; response.embeddings keeps input order.
  const batch = yield* model.embedMany([
    "Staff Data Scientist",
    "Engineering Manager",
    "Product Designer"
  ])
  return {
    backend: backend.vector,
    frontend: frontend.vector,
    count: batch.embeddings.length
  }
}).pipe(
  Effect.provide(
    OpenAiEmbeddingModel.model("text-embedding-3-small", { dimensions: 1536 })
  )
)
```

> **Note:** A vector is only comparable with vectors from the same space. Store the provider, model name, `dimensions`, and your own normalization/schema version beside every embedding, and when any of them changes, build a fresh, separately versioned index from new embeddings.

**Reach for it when** building semantic search, role-matching, deduplication, or RAG retrieval — anything requiring text-to-vector conversion with automatic batching.

## Decision

`effect/ai/Decision` — unstable

A decision definition is plain data: one input `Schema` plus named questions whose answers come from a closed set. `Decision.make({ input, decisions })` builds it; nothing calls a model until [`DecisionModel.decide`](#decisionmodel) answers every decision in the definition in **one** provider call.

Three kinds of question:

| Constructor | Asks | Answer |
| --- | --- | --- |
| `Decision.classify({ instructions, criteria })` | Which label fits; `criteria` maps each label to a description. | `{ label, probabilities, confidence? }` — `label` is the provider's pick and need not be the most probable label. |
| `Decision.rate({ instructions, criteria })` | Where the input sits on an ordered scale; `criteria` lists levels lowest first. | `{ rating, label, probabilities, confidence? }` — `rating` is the probability-weighted position in `[0, levels - 1]` and can fall between two levels; `label` is the most probable level (the first one on a tie). |
| `Decision.probability({ instructions, criteria? })` | How likely a statement about the input holds; `criteria` (an optional `{ false, true }` pair of descriptions) documents both outcomes but can be omitted. | `{ probability }` — the probability of `true`. |

Answer keys and label types are inferred from the definition. The constructors validate eagerly and **throw**: `classify` needs at least two labels, `rate` at least two distinct levels, and `make` at least one decision. Define them at module level so a malformed definition fails at startup, not mid-request. `criteria` on a `probability` decision is optional — code that inspects a definition at runtime should read it as `decision.criteria?.true`, not assume it is present.

Instructions and descriptions accept `Decision.Description`: a string, JSON object, or JSON array. Classification descriptions may also be `null` when the label needs no explanation. `Decision.rate` accepts strings or `{ label, description }` entries, but its returned `Rate.criteria` always contains **objects**: strings normalize to `{ label: value, description: value }`. Code inspecting a definition must read `.label` rather than treating each level as a string. TypeSafe, Cloudflare, and OpenRouter forward structured instructions, descriptions, and score legends; provider behavior remains distinct from core answer validation.

```ts
import { Schema } from "effect"
import { Decision } from "effect/ai"

// The input schema is the contract: `decide` encodes the value with
// Schema.toCodecJson and sends the JSON as the decision state.
export const RaiseRequest = Schema.Struct({
  employeeId: Schema.String,
  level: Schema.Int,
  requestedIncreasePct: Schema.Finite,
  justification: Schema.String
})

// Three judgments about one raise request, answered together.
export const RaiseRequestTriage = Decision.make({
  input: RaiseRequest,
  decisions: {
    basis: Decision.classify({
      instructions: "What the manager's justification mainly rests on",
      criteria: {
        performance: "Documented results or scope beyond the current level",
        market: "Pay below market for the role, or a competing offer",
        retention: "Flight risk or a critical skill without a backup",
        other: "None of the above"
      }
    }),
    evidence: Decision.rate({
      instructions: "How well the justification is supported by specifics",
      criteria: ["unsupported", "anecdotal", "specific", "documented"]
    }),
    needsHrbpReview: Decision.probability({
      instructions: "The request needs an HR business partner's review before approval",
      criteria: {
        false: "A routine request within merit-cycle policy",
        true: "An exception, an out-of-band amount, or sensitive circumstances"
      }
    })
  }
})
```

**Reach for it when** a question has a fixed answer space — routing, triage, policy checks, quality ratings — and you want the answer as a label or number your code can threshold, not as prose.

## DecisionModel

`effect/ai/DecisionModel` — unstable

The provider-neutral service that answers a `Decision` definition. `DecisionModel.decide(definition, { input })` encodes the input with `Schema.toCodecJson` (an explicit `undefined` field becomes `null`; an absent one stays absent), sends that state and every decision to the provider in one request, validates the reply, and returns `{ answers, usage }`. It requires `DecisionModel.DecisionModel` plus the input schema's encoding services, and fails only with `AiError`.

**Images are input fields.** Declare a `Decision.Image` field in the input schema — its value is a `DecisionModel.Image` (`{ mediaType, data }` with base64, bytes, or a `URL`) — and refer to it by its path in instructions. `decide` replaces each image with a reference string in the encoded state and sends the images separately, once each. `Decision.locate({ image: "photo", instructions })` answers with one bounding box around a target in an image field; `Decision.make` checks the path against the input's required image fields, and the answer is a box even when the target is absent, so pair it with a `probability` decision. Providers opt in with `supportsImages: true` and `supportsLocate: true`; others reject images or locate decisions with `InvalidUserInputError` before any network call.

> **Migration:** `DecideOptions.images` is gone. Move images into the input schema as `Decision.Image` fields: `DecisionModel.decide(Triage, { input: { report, screenshot } })`. `Decision.Any` and `DecisionModel.ProviderAnswer` gained `Locate` variants, so exhaustive switches over them need that case, and a custom provider receives `images` as `{ reference, path, image }`.

`decide` is all-or-nothing and also returns the response `id`, the answering `modelId`, and provider `metadata`. `DecisionModel.decideSettled(definition, { input })` keeps valid answers instead: it returns `outcomes`, one `Result` per decision whose failure is an `Unanswered` with reason `Missing`, `Refused`, `NoDistribution`, or `Invalid`; provider failures still fail the effect. With `decide`, a refusal of any decision fails with `ContentPolicyError`.

The reply is validated before your code sees it; the provider is not trusted to follow the contract:

- every decision is answered with its own kind, and a classify label must be one of its criteria keys;
- each distribution covers every label or level and sums to 1 within `1e-6`; `confidence` and `probability` lie in `[0, 1]`; a rating lies in `[0, levels - 1]`;
- a violation fails with reason `InvalidOutputError`, and an input that cannot be encoded fails with `InvalidUserInputError` before any provider call.

Validation proves shape, not calibration: whether a `0.3` means 30% depends on the provider. Choose thresholds from labeled historical cases, and treat them as application policy.

```ts
import { Effect, Layer, Schema } from "effect"
import { Decision, DecisionModel } from "effect/ai"
import { TypeSafeDecisionModel } from "@effect/ai-typesafe"

// Kept self-contained here; a real module would import the definition above.
const RaiseRequest = Schema.Struct({
  employeeId: Schema.String,
  requestedIncreasePct: Schema.Finite,
  justification: Schema.String
})

const RaiseRequestTriage = Decision.make({
  input: RaiseRequest,
  decisions: {
    evidence: Decision.rate({
      instructions: "How well the justification is supported by specifics",
      criteria: ["unsupported", "anecdotal", "specific", "documented"]
    }),
    needsHrbpReview: Decision.probability({
      instructions: "The request needs an HR business partner's review before approval",
      criteria: { false: "Routine, within policy", true: "Exception or sensitive" }
    })
  }
})

type Route = "hrbp-review" | "return-to-manager" | "auto-approve"

// Both answers come from one provider call; the thresholds are HR policy.
const routeRaiseRequest = Effect.fn("routeRaiseRequest")(function*(
  request: typeof RaiseRequest.Type
) {
  const { answers } = yield* DecisionModel.decide(RaiseRequestTriage, { input: request })
  const route: Route = answers.needsHrbpReview.probability >= 0.3
    ? "hrbp-review"
    : answers.evidence.rating < 1.5
    ? "return-to-manager"
    : "auto-approve"
  return route
})

declare const request: typeof RaiseRequest.Type

// Production: a provider's `.model(...)` is a Model Layer, like LanguageModel's.
// It still requires TypeSafeClient (see Provider packages below).
const routed = routeRaiseRequest(request).pipe(
  Effect.provide(TypeSafeDecisionModel.model("jev-latest"))
)

// Tests: a scripted provider. The core validates its answers exactly as it
// does a real provider's, and derives the rating label from the distribution.
const ScriptedDecisions = Layer.effect(
  DecisionModel.DecisionModel,
  DecisionModel.make({
    decide: () =>
      Effect.succeed({
        answers: {
          evidence: {
            _tag: "Rate",
            rating: 2.4,
            probabilities: { unsupported: 0.05, anecdotal: 0.1, specific: 0.25, documented: 0.6 }
          },
          needsHrbpReview: { _tag: "Probability", probability: 0.1 }
        },
        usage: { inputTokens: undefined, outputTokens: undefined }
      })
  })
)

const routedInTest = routeRaiseRequest(request).pipe(Effect.provide(ScriptedDecisions))
```

`DecisionModel.make({ decide, probabilityPrecision?, supportsImages?, supportsLocate? })` is also how to adapt an unsupported provider: `decide` receives `{ state, decisions, images? }` and returns answers tagged `"Classify"`, `"Rate"`, `"Probability"` (or `"Locate"` when `supportsLocate: true`), optional `probabilities`, refusal answers, and token `usage`; the service it builds implements both `decide` and `decideSettled`. Returned answers and their probability records have `null` prototypes, so compare fields rather than deep-equality against an object literal.

A provider that rounds each probability to a fixed number of decimal places can return a distribution whose sum drifts slightly from 1. `probabilityPrecision` tells `make` how many decimal places the provider rounds to, so it accepts drift up to half a unit of the last place per label and rescales the distribution to sum to 1 instead of failing validation; a larger drift still fails with `InvalidOutputError`. The OpenRouter and TypeSafe providers both set `probabilityPrecision: 2`.

**DecisionModel or `generateObject`?**

| Need | Use |
| --- | --- |
| A label, a level, or a likelihood from a fixed set, with a probability distribution to threshold on | `DecisionModel` |
| Several such judgments over the same input, in one call and one validated response | `DecisionModel` with several named decisions |
| Free-form fields, a rationale, nested data, a system prompt or conversation history, or tool calls | `LanguageModel.generateObject` / `generateText` |
| A provider that has no decisions API | `LanguageModel.generateObject` with a `Schema.Literals` field |

A `confidence` field in a `generateObject` schema is text the model wrote about itself; a `DecisionModel` answer is a range-checked probability, or a distribution the core checked for completeness and normalization. The trade is expressiveness: a decision has only its per-question `instructions` and `criteria`, no prompt, history, or tools.

**Reach for it when** routing, triage, or scoring must be a number your code can threshold and audit. Keep `LanguageModel` for anything that needs words, structure, or tools.

## Model

`effect/ai/Model` — unstable

The provider-agnostic handle every provider's `.model(...)` returns. A `Model` is a `Layer` that supplies AI services (`LanguageModel`, `EmbeddingModel`/`Dimensions`, or `DecisionModel`) and records two context values: `Model.ProviderName` and `Model.ModelName`.

`Model.make(provider, name, layer)` wraps any Layer producing a `LanguageModel` into a labeled, providable handle — useful for adapters (an in-house gateway, a self-hosted model) not covered by the [provider packages](#provider-packages). Use `model.captureRequirements` to fold the provider's client requirement into a service Layer; read `Model.ProviderName`/`ModelName` to log or branch on which model ran.

```ts
import { Effect, Layer } from "effect"
import { LanguageModel, Model } from "effect/ai"

declare const gatewayLayer: Layer.Layer<LanguageModel.LanguageModel>

// Wrap any LanguageModel layer into a labeled, provider-agnostic handle.
const inHouse = Model.make("acme-gateway", "review-drafter-v3", gatewayLayer)

const program = Effect.gen(function*() {
  const provider = yield* Model.ProviderName // "acme-gateway"
  const name = yield* Model.ModelName      // "review-drafter-v3"
  yield* Effect.log(`drafting review summary with ${provider}/${name}`)
  return yield* LanguageModel.generateText({ prompt: "Summarize this review." })
}).pipe(Effect.provide(inHouse))
```

**Reach for it when** you need an unsupported provider, or want to read/log which provider+model handled a request.

## Tokenizer

`effect/ai/Tokenizer` — unstable

A service for counting tokens and truncating a `Prompt` to a token budget. `tokenize(input)` returns the token array (use `.length` for a count); `truncate(input, maxTokens)` drops whole messages from the front until the prompt fits.

`Tokenizer.make({ tokenize })` builds the service from a single tokenizing function — wrap a real provider tokenizer (e.g. tiktoken) or a cheap word-splitter for tests. `truncate` is implemented on top of `tokenize` by default.

**`truncate` keeps the newest messages.** It walks backward from the last message, re-tokenizes the whole retained suffix each time (so tokens the tokenizer charges *between* messages count against the budget), and stops at the first message that would overflow. It never splits a message, and it does not pin a leading system message — re-apply the system prompt with `Prompt.setSystem` after truncating when the persona must survive. Token counts from your tokenizer are an estimate for budgeting; the provider's reported `usage` is the reconciliation.

```ts
import { Effect } from "effect"
import { Tokenizer } from "effect/ai"

// Estimate the size of a review packet before sending it to the model.
const countTokens = Effect.gen(function*() {
  const tokenizer = yield* Tokenizer.Tokenizer
  const tokens = yield* tokenizer.tokenize("Q3 self-review and manager feedback…")
  return tokens.length
})

// A simple word-based tokenizer service (swap in a real BPE one for prod).
const WordTokenizer = Tokenizer.make({
  tokenize: (prompt) =>
    Effect.succeed(
      prompt.content
        .flatMap((msg) =>
          typeof msg.content === "string"
            ? msg.content.split(" ")
            : msg.content.flatMap((p) => (p.type === "text" ? p.text.split(" ") : []))
        )
        .map((_, i) => i)
    )
})
```

**Reach for it when** you need to stay under a context window, estimate cost before a call, or trim long histories before sending.

## IdGenerator

`effect/ai/IdGenerator` — unstable

A service that mints identifiers for AI artifacts, primarily tool-call IDs. Uses Effect's `Random` under the hood, so it is deterministic under a seeded test runtime.

`IdGenerator.layer({ alphabet, prefix, separator, size })` produces IDs like `tool_A1B2C3D4`. The framework uses a sensible default; override to match a provider's expected ID format or to get reproducible IDs in tests via a custom `Service`.

```ts
import { Effect } from "effect"
import { IdGenerator } from "effect/ai"

const useIds = Effect.gen(function*() {
  const gen = yield* IdGenerator.IdGenerator
  return yield* gen.generateId() // e.g. "tool_A1B2C3D4"
})

// Configure the format, then provide it as a layer.
const program = useIds.pipe(
  Effect.provide(IdGenerator.layer({
    alphabet: "0123456789ABCDEF",
    prefix: "tool",
    separator: "_",
    size: 8
  }))
)
```

**Reach for it when** you need stable, formatted, or deterministic tool-call IDs in tests, or a specific ID shape a provider expects.

## Telemetry

`effect/ai/Telemetry` — unstable

Helpers that write standardized GenAI attributes onto OpenTelemetry spans, following the current OTel GenAI semantic conventions (provider name, model, temperature, token usage, etc.). AI calls are already traced; this enriches those spans.

`addGenAIAnnotations(span, { provider, operation, request, response, usage })` stamps the correct attribute keys (it mutates the span). The provider is written as `gen_ai.provider.name` from `provider: { name }`; the older `system` option, `gen_ai.system`, and `gen_ai.token.type` were removed in 4.0.2. Provide a `CurrentSpanTransformer` to automatically annotate every AI span with custom logic. Pairs with `@effect/opentelemetry` for export.

```ts
import { Effect } from "effect"
import { Telemetry } from "effect/ai"

// Stamp GenAI attributes on the span around a review-drafting call so cost and
// token usage show up on your comp-tooling dashboards.
const annotated = Effect.gen(function*() {
  const span = yield* Effect.currentSpan
  Telemetry.addGenAIAnnotations(span, {
    provider: { name: "openai" },
    request: { model: "gpt-5.2", temperature: 0.7 },
    usage: { inputTokens: 100, outputTokens: 50 }
  })
})
```

**Reach for it when** running AI in production and needing spans/metrics aligned with GenAI OTel conventions for cost dashboards, latency, and token tracking.

## AiError

`effect/ai/AiError` — unstable

The typed failure channel for all AI operations. Every provider call, tool invocation, and structured decode fails with an `AiError` carrying a structured `reason`, enabling LLM failures to be handled like any other Effect error.

One umbrella error (`AiError`, tag `"AiError"`) wrapping a discriminated `reason`: `RateLimitError`, `QuotaExhaustedError`, `AuthenticationError`, `ContentPolicyError`, `InvalidRequestError`, `InternalProviderError`, `NetworkError`, `InvalidResponseError` (the provider's response or SSE stream did not decode), `InvalidOutputError`/`StructuredOutputError` (bad/unparseable model output), plus tool errors (`ToolNotFoundError`, `ToolParameterValidationError`, …). `AiError.AiErrorReason` is itself a `Schema`, so it can be embedded in domain error types.

```ts
import { Effect, Schedule, Schema } from "effect"
import { AiError, LanguageModel } from "effect/ai"

// Wrap provider failures in your own tagged error, reusing the reason schema.
class ReviewServiceError extends Schema.TaggedError<ReviewServiceError>()("ReviewServiceError", {
  reason: AiError.AiErrorReason
}) {}

const draftSummary = LanguageModel.generateText({
  prompt: "Summarize this employee's review."
}).pipe(
  // Retry only transient rate-limit failures, and only a bounded number of
  // times: `upTo` caps both the recurrence count and the total backoff window.
  Effect.retry({
    while: (error) => error.reason._tag === "RateLimitError",
    schedule: Schedule.exponential("200 millis").pipe(
      Schedule.upTo({ times: 3, duration: "10 seconds" })
    )
  }),
  // One absolute deadline for the call and all of its retries.
  Effect.timeout("30 seconds"),
  // Translate anything that still fails into our domain error.
  Effect.catchTag("AiError", (error) =>
    Effect.fail(new ReviewServiceError({ reason: error.reason }))
  )
)
```

### Classifying a failure before retrying

Every reason class has an `isRetryable` getter, and `AiError` forwards it (`error.isRetryable`) together with `error.retryAfter` — the provider's requested delay when the reason is a `RateLimitError` that carried one.

| `reason._tag` | `isRetryable` | What to do |
| --- | --- | --- |
| `RateLimitError` | `true` | Back off; honor `retryAfter` when present. |
| `InternalProviderError`, `NetworkError` with `reason: "TransportError"` | `true` | Bounded retry. |
| `InvalidOutputError`, `StructuredOutputError`, `ToolNotFoundError`, `ToolParameterValidationError` | `true` | The model produced something unusable; a retry re-bills the whole prompt, so cap it tightly. |
| `AuthenticationError`, `QuotaExhaustedError`, `ContentPolicyError`, `InvalidRequestError`, `InvalidResponseError`, `UnsupportedSchemaError`, `InvalidToolResultError`, `ToolResultEncodingError`, `ToolConfigurationError`, `ToolkitRequiredError`, `InvalidUserInputError`, `UnknownError`, other `NetworkError` reasons | `false` | Fix credentials, quota, input, or code; do not loop. |

- **Retry only before output or side effects exist.** Never retry after streamed text has reached the caller or after a mutating tool was dispatched; count every retry against the same token and time budget as the first attempt.
- **A fallback model is a different capability, not a retry.** Re-validate tool, schema, and media support for it, and never use a fallback to get around a `ContentPolicyError`.
- **`AuthenticationError` has an optional `description`.** On HTTP 401/403 the provider's own error text is passed through and appended to the kind-based hint, so logs say what was actually rejected. HTTP-derived reasons carry `AiError.HttpContext` — the optional `response` (`AiError.HttpResponseDetails`: status and headers) and `body` — but **no request**: AI errors and `Response.ResponseMetadataPart` never hold request headers or query parameters.
- **`NetworkError` records `method` and `url`**, plus an optional `cause` holding the original `HttpClientError` unchanged and **not redacted**. `NetworkError.fromHttpClientError` sets it (all providers use it); `NetworkError.fromRequestError` does not. Log the `cause` as carefully as the request itself.
- **`InvalidResponseError` is not retryable**: a response that fails the provider's schema will fail again. Model-generated output that fails validation stays a retryable `InvalidOutputError`.

> **Migration:** `AiError.HttpRequestDetails` and `Response.HttpRequestDetails` are removed, and `request` is gone from `AiError.HttpContext`, `NetworkError`, and `Response.ResponseMetadataPart`; read `error.reason.method` / `error.reason.url` on a `NetworkError`. Exhaustive matches on `AiErrorReason` need an `InvalidResponseError` case, and consumers of serialized errors must accept it.

**Reach for it when** you need robust error handling — retry on rate limits, surface auth/quota problems, or translate provider failures into domain error types.

## ResponseIdTracker

`effect/ai/ResponseIdTracker` — unstable

An optimization service for providers that support continuing from a prior response (e.g. OpenAI's Responses API `previousResponseId`). Records which prompt messages were sent with each response; a follow-up call can send only the new messages plus the prior response ID.

`markParts(parts, responseId)` records what produced a response; `prepareUnsafe(prompt)` returns an `Option` of `{ previousResponseId, prompt }` — the untracked suffix after the last assistant turn — when the prefix is fully recognized. Provide it as a Layer; compatible providers use it transparently to shrink request payloads.

```ts
import { Effect } from "effect"
import { Chat, ResponseIdTracker } from "effect/ai"

// Provide the tracker so a compatible provider reuses previousResponseId
// instead of re-sending the entire HRBP conversation each turn.
const withTracking = Effect.gen(function*() {
  const tracker = yield* ResponseIdTracker.make
  const chat = yield* Chat.fromPrompt("You are an HR policy assistant.")
  const provideTracker = Effect.provideService(
    ResponseIdTracker.ResponseIdTracker,
    tracker
  )

  yield* chat.generateText({ prompt: "Summarize our promotion policy." }).pipe(provideTracker)
  return yield* chat.generateText({ prompt: "Now list the exceptions." }).pipe(provideTracker)
})
```

Run the whole conversation with one compatible `LanguageModel` service. The same tracker instance must span the calls; constructing a tracker and returning it without providing `ResponseIdTracker.ResponseIdTracker` has no effect. OpenAI's WebSocket-mode integration (`OpenAiClient.layerWebSocketMode` / `withWebSocketMode`) installs a tracker; do not assume ordinary provider layers do.

**Reach for it when** running long multi-turn sessions against a provider that supports incremental continuation, to cut bandwidth and latency by not re-sending history.

## AnthropicStructuredOutput

`effect/ai/AnthropicStructuredOutput` — unstable

The adapter enabling `generateObject` with Anthropic. `toCodecAnthropic(schema)` converts an Effect `Schema.Codec` into the JSON Schema subset Anthropic accepts and a matching codec to decode the model's reply back into the application type. The Anthropic provider wires this in automatically as its `codecTransformer`.

Schema rewriting is lossless where possible: tuples become numeric-key objects, records become `[key, value]` arrays, optional props become nullable required props (a prop that already accepts `null` keeps a single `null` branch), `oneOf` becomes `anyOf`. The returned codec decodes exactly the format it advertises, including optional records and tuples, and a root `$ref` is followed through chains of identified aliases. Unsupported shapes throw at conversion time. Call directly only when you need the raw JSON Schema for a custom Anthropic request.

**Reach for it when** you need Anthropic-compatible JSON Schema by hand. For normal use, call `generateObject` with the Anthropic provider — this runs automatically.

## OpenAiStructuredOutput

`effect/ai/OpenAiStructuredOutput` — unstable

The OpenAI counterpart. `toCodecOpenAI(schema)` turns an Effect `Schema.Codec` into OpenAI's structured-output JSON Schema subset plus a decoding codec. The OpenAI provider uses it automatically for `generateObject` and for the parameters of strict tools (strict unless the tool sets `Tool.Strict` to `false` or the model config sets `strictJsonSchema: false`); non-strict tools are advertised with their standard JSON Schema, so records and optional properties keep their natural shape.

OpenAI-specific rewriting: `allOf` is flattened (OpenAI does not support it) and multiple regex filters are merged into one `pattern`. The same optional-property, optional record and tuple, and root-`$ref` rules as the Anthropic adapter apply. Unsupported schema kinds fail loudly at conversion. Use directly only to generate JSON Schema for a bespoke OpenAI call.

**Reach for it when** you need OpenAI-compatible JSON Schema directly. Otherwise, `generateObject` with the OpenAI provider already uses it.

## WorkloadIdentity

`effect/ai/WorkloadIdentity` — unstable

Authenticates provider calls without a long-lived API key: the workload's own identity token (a projected Kubernetes service-account token, a cloud OIDC token) is exchanged at a token endpoint for a short-lived access token. `WorkloadIdentity.make({ tokenUrl, identityToken, exchangeRequest, defaultExpiresIn? })` returns, in an Effect that requires an `HttpClient`, an `HttpClient` transform that sends `Authorization: Bearer <access token>` on every request. Use the core module only for a token endpoint the provider packages do not cover; for the providers below, use their module:

| Package module | Options | Exchange |
| --- | --- | --- |
| `AnthropicWorkloadIdentity` (`@effect/ai-anthropic`) | `federationRuleId`, `organizationId`, optional `serviceAccountId`, `workspaceId`, `apiUrl` | `jwt-bearer` grant at `${apiUrl}/v1/oauth/token`; adds the `oauth-2025-04-20` beta header |
| `OpenAiWorkloadIdentity` (`@effect/ai-openai`) | `identityProviderId`, `serviceAccountId`, optional `identityTokenType` (`"jwt"` or `"id_token"`), `tokenUrl` | `token-exchange` grant at `https://auth.openai.com/oauth/token` |
| `OpenRouterWorkloadIdentity` (`@effect/ai-openrouter`) | `federationPolicyId`, optional `apiUrl` | `token-exchange` grant with the `inference` scope at `${apiUrl}/oauth/token` |

Each provider module has `make(options)`, whose transform goes in the client's `transformClient` option, and `layer(options)`, which replaces the current `HttpClient` with an authenticated one. Leave the client's `apiKey` unset either way.

- **Lifetime.** `identityToken` (an `Effect` of a `Redacted` string) runs again on every exchange, so a rotated token file is picked up. The access token is cached until 30 seconds before expiry or three quarters of its lifetime, whichever is later; concurrent requests share one exchange. There is no background refresh, the exchange is never retried, and a `401` drops only the token that request used, without replaying it. Anthropic accepts a token with a `jti` claim only once, so its `identityToken` must produce a fresh one each time.
- **Failures** are `HttpClientError`s (`StatusCodeError`, `TransportError` for transport failures and redirects, `DecodeError` for a bad token response, `EncodeError` for a failing or empty identity token), which the provider clients map to `AiError`. The literal identity token is redacted from errors; keep secrets out of failures raised by `identityToken` itself.
- **Scope the layer.** The transform adds the access token to every request regardless of URL, so provide `layer(...)` only to the provider client, never to the application's shared `HttpClient`. The exchange uses the `HttpClient` captured at construction, must not follow redirects (redirects fail the exchange), and must not log request bodies. A token URL that is not `https` (or `http` on `localhost`, `127.0.0.1`, `::1`) is a defect.
- **WebSocket mode** (OpenAI) resolves the access token when each socket connects and keeps it for the socket's lifetime.

```ts
import { AnthropicClient, AnthropicLanguageModel, AnthropicWorkloadIdentity } from "@effect/ai-anthropic"
import { Effect, FileSystem, Layer, Redacted } from "effect"

// Exchange the pod's projected token for an Anthropic access token.
const AnthropicIdentity = AnthropicWorkloadIdentity.layer({
  identityToken: Effect.gen(function*() {
    const fs = yield* FileSystem.FileSystem
    return Redacted.make(yield* fs.readFileString("/var/run/secrets/anthropic.com/token"))
  }),
  federationRuleId: "fdrl_...",
  organizationId: "00000000-0000-0000-0000-000000000000",
  serviceAccountId: "svac_..."
})

// No apiKey. Still requires an HttpClient and a FileSystem from the platform.
const AnthropicLive = AnthropicLanguageModel.model("claude-opus-4-6").pipe(
  Layer.provide(AnthropicClient.layer({}).pipe(Layer.provide(AnthropicIdentity)))
)
```

**Reach for it when** a deployment's security policy forbids static provider keys and the platform can issue workload identity tokens.

## AgUiSchema

`effect/ai/AgUiSchema` — experimental

Schemas for the [Agent User Interaction (AG-UI)](https://docs.ag-ui.com) protocol, version `AgUiSchema.protocolVersion` (`"1.0"`): the `RunAgentInput` request body a frontend posts (`threadId`, `runId`, `messages`, `tools`, `context`, optional `state`, `forwardedProps`, `resume`), the conversation `Message` union (developer, system, user, assistant, tool, activity, reasoning), and the `Event` union an agent streams back (`RUN_*`, `STEP_*`, `TEXT_MESSAGE_*`, `TOOL_CALL_*`, `REASONING_*`, `STATE_*`, `SUBAGENT_*`, and more).

`AgUiSchema.Message` describes AG-UI **wire** messages. It does not replace `Prompt.Message`: convert a run's conversation with `AgUi.toPrompt` before calling a model.

**Reach for it when** decoding AG-UI input or emitting hand-written AG-UI events; most servers only touch it through `AgUi` and `AgUiServer`.

## AgUi

`effect/ai/AgUi` — experimental

Pure conversions between `effect/ai` values and AG-UI; no I/O.

- **`AgUi.toPrompt(input, { trustClientInstructions? })`** turns a run's conversation into a `Prompt`: user and assistant text, assistant tool calls with parsed JSON arguments, tool messages as tool results (failed when they carry an `error`), and user media with inline data or a valid URL as file parts. Activity and reasoning messages, and tool messages that answer no earlier call, are dropped. **By default `system` and `developer` messages and the input's `context` are dropped too**, because the whole conversation comes from the browser; set `trustClientInstructions: true` only when the client is trusted to steer the model.
- **`AgUi.fromStreamParts(stream)`** maps `LanguageModel.streamText` parts to events: each model round becomes a step (`step-1`, `step-2`, …), text and reasoning parts become message events, tool calls become `TOOL_CALL_START` / `ARGS` / `END`, final tool results become `TOOL_CALL_RESULT` (failed ones marked with `{ "effect/ai": { isFailure: true } }` metadata). An `error` part is logged and becomes a `RUN_ERROR` with a generic message; preliminary results, approval requests, files, sources, and response metadata are dropped. It requires `IdGenerator`.
- **`AgUi.withRunLifecycle({ threadId, runId })`** wraps any event stream so the client sees `RUN_STARTED` first and exactly one `RUN_FINISHED` or `RUN_ERROR` last, closing any message, tool call, step, or subagent left open. A failing stream is logged, reported to the `ErrorReporter`, and ends with a `RUN_ERROR` without details; to send a specific error, emit a `RUN_ERROR` event instead of failing.

**Reach for it when** you serve AG-UI over a transport other than `AgUiServer`, or need to post-process the events.

## AgUiServer

`effect/ai/AgUiServer` — experimental

Serves AG-UI runs over HTTP: clients `POST` a JSON `RunAgentInput` and read the run as Server-Sent Events (`Cache-Control: no-cache`, a keepalive comment every 15 seconds). An invalid body is answered with `400` before the stream opens.

- **`AgUiServer.layerHttp({ path, agent })`** or **`layerHttp({ path, run })`** mounts one run handler on the current `HttpRouter`; it also needs the services `HttpApiBuilder.layer` needs (`Etag.Generator`, `FileSystem`, `HttpPlatform`, `Path`), which platform HTTP server layers provide. `endpoint(name, path)` with `handler(run)` or `agentHandler(agent)` adds the same route to an existing `HttpApi`.
- **`AgUiServer.agent({ toolkit, system?, maxSteps? })`** serves the current `LanguageModel`. The prompt is your `system` text followed by `AgUi.toPrompt(input)`; the run's frontend tools are offered as [client-executed tools](#client-executed-tools) next to your server toolkit, and a run whose tool names collide with a server tool gets `400`. Rounds follow [`Chat.streamText` with `maxSteps`](#chat) (one round by default). `RUN_FINISHED` reports usage summed over every round and lists unanswered frontend calls as `pendingToolCallIds`; the frontend answers them with tool messages in its next run. Server tool handlers can read the run's input from `AgUiServer.RunInput` by declaring it in `dependencies`.
- A `run` stream is wrapped with `AgUi.withRunLifecycle`, and an event that cannot be encoded as JSON becomes a sanitized `RUN_ERROR`.

Every field of the input — messages, tools, state, forwarded props — is client-controlled. Authenticate the route like any other HTTP endpoint, authorize inside server tool handlers, and bound `maxSteps`.

```ts
// @effect-diagnostics experimentalApiUsage:off
import { Effect, Schema } from "effect"
import { AgUiServer, Tool, Toolkit } from "effect/ai"

const LookupBand = Tool.make("LookupBand", {
  description: "Return the salary band midpoint for a job level",
  parameters: Schema.Struct({ level: Schema.Int }),
  success: Schema.Struct({ mid: Schema.Finite })
})
const CompTools = Toolkit.make(LookupBand)

// POST /agent streams AG-UI events; frontend tools from the request are client-executed.
export const AgentRoute = AgUiServer.layerHttp({
  path: "/agent",
  agent: AgUiServer.agent({
    toolkit: CompTools,
    system: "You are a compensation assistant. Use LookupBand for every band figure.",
    maxSteps: 4
  })
})

// Provide with a LanguageModel, an HttpRouter, and the platform's HTTP services.
export const CompToolsLive = CompTools.toLayer({
  LookupBand: ({ level }) => Effect.succeed({ mid: 150_000 + level * 10_000 })
})
```

**Reach for it when** a browser frontend that speaks AG-UI should drive an Effect agent with streaming text, tool calls, and frontend tools.

## Production rules for model calls

The modules above make a model call typed; they do not make it safe. These rules are the short form of [Building a Production AI Capability](../deep-dives/building-a-production-ai-capability.md), which works through each with code.

| Concern | Rule | Mechanism on this page |
| --- | --- | --- |
| Trust | **Treat user text, retrieved text, prior model output, tool arguments, and tool results as untrusted**; take actor, tenant, approval, idempotency key, and policy version only from trusted request context, never from model output. | Handler closes over trusted services; Schemas validate shape, not authority. |
| Tool execution | **Bound tool concurrency and keep mutations behind your own gate.** | `concurrency`, `disableToolCallResolution`, `toolkit.handle`, `setNeedsApproval`. |
| Loops | **Every agent loop has a turn limit and a typed exhaustion error.** | Bounded `for` loop around `Chat.generateText`. |
| Budgets | **Every limit is finite and checked before dispatch**: estimate prompt tokens and reserve worst-case output before the call; treat returned `usage` as reconciliation. | `Tokenizer`, `response.usage`, `Effect.timeout`. |
| Retries | **Classify by `reason`, bound the schedule, and stop once output or a side effect exists.** | `error.isRetryable`, `error.retryAfter`, `Schedule.upTo`. |
| Structured output | **The local `Schema` decode is authoritative** even when the provider advertises strict JSON; never fall back to parsing prose. | `generateObject`, `StructuredOutputError`. |
| Dynamic and MCP tools | **Allow-list tool names, snapshot and validate their schemas locally, and re-review when a server changes them.** | `Tool.dynamic`, `Tool.getJsonSchema`, `McpClient.toolkit(client, { tools, transformTool })`. |
| Media input | **Validate MIME type, size, count, and URI scheme before a file part leaves the process**; never let a prompt trigger an implicit URL fetch. | `Prompt` file parts. |
| Telemetry | **Record model, provider, latency, finish reason, usage, and tool names; keep prompts, keys, and reasoning text out of spans and logs.** | `Telemetry`, `Model.ProviderName` / `ModelName`, `Config.Redacted`; leave `Toolkit.RecordToolArguments` off. |

**Testing checklist.** Provide a scripted `LanguageModel` built with `LanguageModel.make` (it can record prompts and replay tool calls, failures, or a stream that never ends) and assert: a bounded multi-turn run succeeds; an unauthorized or unapproved call reaches **zero** handlers; a repeated call id produces one side effect; each budget fails at its exact edge; malformed tool arguments and unknown tool names fail closed; an interrupted stream leaves a recorded incomplete turn and runs its finalizers. Fakes prove policy, recorded HTTP fixtures prove adapter parsing, and a small separately authorized live suite proves only that the remote API still behaves as recorded.

## Provider packages

Primitives live in `effect/ai`; concrete providers ship as satellite packages. Each exposes a `Client.layerConfig(...)` that reads its credentials from `Config` (requires an `HttpClient`) and one or more `.model(name)` constructors (each a `Model` Layer). Anthropic, OpenAI, and OpenRouter clients can also authenticate with [workload identity](#workloadidentity) instead of an API key. Every language-model provider produces the same `LanguageModel` service, and every decision provider the same `DecisionModel` service; switching providers is a one-line Layer change.

**Provider upgrade checks.** OpenAI treats unknown model names as reasoning models by default (fine-tunes follow their base model), selects the `developer` role, and merges explicit includes with automatic encrypted-reasoning includes. Set `reasoningModel: false` for a custom deployment that needs the `system` role and no automatic reasoning include. Responses streaming accepts a `[DONE]` sentinel; `find_in_page` web-search actions can omit both `url` and `pattern`, so do not assume either field exists. The regenerated `Generated` surface removes unreferenced Realtime WebSocket exports, renames multipart bodies to `*Multipart` / `*FormData`, and gives maps/free-form objects record types; recompile direct generated-client users rather than treating this patch upgrade as type-neutral. Anthropic fallback blocks survive in `metadata.anthropic.fallback`, with fallback model attribution in usage iterations. Cloudflare decisions support inline bytes, base64, and data URLs, but reject remote image URLs before dispatch. Truncation is reported as `finishReason: "length"` when OpenAI stops at `max_output_tokens` or Anthropic at `model_context_window_exceeded`. Every provider parses `Retry-After` as whole non-negative seconds or an HTTP date, so `retryAfter` is never negative or infinite. OpenAI-compatible and OpenRouter refusals finish with reason `"content-filter"` and carry the model's explanation as `refusal` in the finish part's provider metadata (`metadata["openai-compat"]` / `metadata.openrouter`); a refusal is not an `AiError`, so check the finish reason before trusting the text.

- **pkg @effect/ai-openai** — OpenAI Responses API. `OpenAiClient.layerConfig`, `OpenAiLanguageModel.model("gpt-5.2")`, `OpenAiEmbeddingModel.model(name, { dimensions })`, provider-defined tools via `OpenAiTool` (e.g. `OpenAiTool.WebSearch`), and `OpenAiTelemetry`. A web search `search` action's `sources` can be URL sources (`{ type: "url", url }`) or API sources (`{ type: "api", name }`, such as `oai-weather`), so narrow each source on `type` before reading `url` or `name`; `action` itself is optional on a web-search call or result, and an incomplete call decodes as a failed tool result instead of failing to decode. A dynamic tool backed by raw JSON Schema keeps its tool-call parameters intact. Prompt caching can be steered explicitly: set `options: { openai: { promptCacheBreakpoint: { mode: "explicit" } } }` on a system message or a text part to mark the end of a reusable prefix (the provider documents this for GPT-5.6 or later and may reject it on earlier models). In websocket mode, a turn that gets back `previous_response_not_found` is retried once with the full prompt, keeping the socket open if it is still open, and a failing `transformClient` (for example a workload-identity exchange) fails the turn with an `AiError` rather than a defect.

- **pkg @effect/ai-anthropic** — Anthropic Messages API. `AnthropicClient.layerConfig`, `AnthropicLanguageModel.model("claude-opus-4-6")`, `AnthropicTool`, and `AnthropicTelemetry`. Structured output bridged automatically through `AnthropicStructuredOutput`. Multiple system instructions are preserved across a conversation; the optional `LanguageModel.supportsSystemMessagesInHistory` flag on the provider controls whether mid-conversation system messages are forwarded (Anthropic sets this to `true`). Cached tokens are included in `gen_ai.usage.input_tokens` and reported separately as `gen_ai.usage.cache_read.input_tokens` and `gen_ai.usage.cache_write.input_tokens`; the former per-response `cacheCreationInputTokens` and `cacheReadInputTokens` options were removed in 4.0.2. A malformed 4xx response from an Anthropic-compatible gateway is mapped to the matching `AiError` reason by HTTP status (including a 429's retry delay) instead of failing as `InvalidOutputError`; a direct `AnthropicClient.client.betaMessagesPost` call fails with `HttpClientError` rather than `SchemaError` for these responses. A message response that omits `usage.inference_geo` — as some Anthropic-compatible gateways do — still decodes. `output_config.effort` accepts `"low"` through `"xhigh"` and `"max"`. For a large catalog, annotate user tools with `.annotate(AnthropicTool.DeferLoading, true)` next to `AnthropicTool.ToolSearchRegex_20251119()` or `ToolSearchBM25_20251119()`, so a tool is loaded only when the model searches for it; Anthropic requires at least one non-deferred tool (normally the search tool), and the annotation has no effect on provider-defined tools. The `TextEditor_20250429` and `TextEditor_20250728` tools decode `insert` commands with `TextEditorInsertTextCommand` (`insert_text`); the earlier versions keep `TextEditorInsertCommand` (`new_str`). With native structured output, a `generateObject` turn that calls tools keeps its `"tool-calls"` finish reason.

  > **Migration:** `AnthropicTool.ToolSearchRegexParameters` has `pattern` (plus an optional `limit`) instead of `query`; rename the field in code that builds or reads regex tool-search inputs.

- **pkg @effect/ai-amazon-bedrock** — Amazon Bedrock Runtime through the Converse and ConverseStream APIs, with no runtime dependency beyond `effect`. `AmazonBedrockClient.layerConfig()` reads `AWS_REGION` (default `us-east-1`) and either `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` (plus `AWS_SESSION_TOKEN` for temporary keys), signed with SigV4, or a Bedrock API key in `AWS_BEARER_TOKEN_BEDROCK`, sent as a bearer token; `AmazonBedrockClient.layer({ region, apiKey | credentials | accessKeyId + secretAccessKey })` takes them explicitly, in that order of precedence, and without any of them every request fails with an `AuthenticationError` of kind `MissingKey`. `credentials` is an Effect resolved before **every** request, where an AWS SDK provider chain plugs in; build the layer in `Layer.unwrap` around `Effect.cachedWithTTL` so it is not resolved per call. Besides an `HttpClient`, the client requires the `Digest` and `Mac` services of `effect/crypto` for signing (`Digest.layerWebCrypto` plus `Mac.layerWebCrypto`, which itself needs `SecureRandom.layerWebCrypto`, or one platform layer such as `NodeCrypto.layer`), even with an API key. `AmazonBedrockLanguageModel.model(modelId, config?)` or `.layer({ model, config })` provides `LanguageModel` with provider name `"amazon-bedrock"`; `AmazonBedrockLanguageModel.withConfigOverride` sets per-call Converse fields such as `inferenceConfig` and `additionalModelRequestFields` (extended thinking). `generateObject` uses a forced tool by default; set `structuredOutputs: true` in the config to use Converse's native `outputConfig.textFormat` on models that support it. Prompt caching is opted into per message or part with `options: { "amazon-bedrock": { cachePoint: { type: "default" } } }`; reasoning blocks round-trip with their signature, and image and document file parts are sent as bytes. Bedrock exceptions map to `AiError` reasons, and a stream fails on an exception frame such as `throttlingException`. `AmazonBedrockClient`'s `client` member is the authenticated `HttpClient` for Bedrock Runtime operations the service does not model; `AmazonBedrockSchema` holds the Converse wire schemas.

- **pkg @effect/ai-openrouter** — OpenRouter's unified gateway to many models. `OpenRouterClient.layerConfig` (supports `siteReferrer`/`siteTitle` for attribution) and `OpenRouterLanguageModel.model(name)` — one key, hundreds of models. Its model is typed and reported with provider name `"openrouter"` (it was `"openai"`; update explicit `AiModel.Model<"openai", ...>` annotations or provider comparisons). It also offers `OpenRouterDecisionModel.model(name)`, a [`DecisionModel`](#decisionmodel) over OpenRouter's alpha Decisions API; it requires full label and level distributions, and accepts only a string, object, or array as the encoded input (other JSON values fail with `InvalidUserInputError`). Streams that end before a tool call's parameters are complete (for example at the token limit) fail with `ToolParameterValidationError` instead of dropping the call, empty trailing call fragments are ignored, and a response with tool calls but no finish reason finishes with `"tool-calls"`. The regenerated `Generated` client follows the current OpenRouter specification: some intermediate schema exports are gone (derive their types from the request or response schema that contains them), and hand-built fixtures need newly required fields.

- **pkg @effect/ai-typesafe** — A `DecisionModel`-only provider for TypeSafe's System One API; it has no `LanguageModel`. `TypeSafeClient.layerConfig()` reads `TYPESAFE_API_KEY` by default (pass `apiKey` / `apiUrl` Configs to override), and `TypeSafeDecisionModel.model("jev-latest")` provides the service; versioned identifiers such as `"jev-1.13.0"` also work. The client does not retry, and a rate-limit error carries the provider's retry delay when one is sent, so apply the [retry rules](#classifying-a-failure-before-retrying) yourself.

- **pkg @effect/ai-openai-compat** — Any OpenAI-compatible endpoint (local LLMs, Together, Groq, vLLM…). Same `OpenAiClient`/`OpenAiLanguageModel`/`OpenAiEmbeddingModel` API — point `apiUrl` at your server. Tool `toModelOutput` files are sent as text placeholders, and concurrent `embed` calls with different configuration overrides or client transforms are batched separately. System instructions use the `developer` role only for OpenAI reasoning model ids (`o` plus a digit such as `o3`, `gpt-5*`, `codex-*`, `computer-use*`); everything else, including provider-prefixed ids such as `openai/gpt-oss-120b`, gets `system`. PDF file parts are sent as Chat Completions `file` content parts from bytes, base64, data URLs, or a configured file ID; a PDF **URL** fails with `InvalidUserInputError` before dispatch. Optional response fields that a server sends as `null` decode, and the affected `OpenAiClient` response types include `null`.

  Its provider augmentation key is **`"openai-compat"`**, not `"openai"`: use it for error metadata, prompt part options, and response part metadata (including `extraContent` thought signatures). Migrate custom code that reads or writes those provider-specific fields; the package no longer shares augmentation keys with `@effect/ai-openai`. Chat Completions does not support the removed `store` / `conversation` options. Assistant reasoning is replayed together with text and tool calls, including parts that carry item ids.

- **OpenAI decisions** — `OpenAiDecisionModel.model("gpt-6-luna")` from `@effect/ai-openai` provides `DecisionModel` over the Decisions API; `OpenAiClient.createDecisions` is the lower-level call. Refusals are reported per decision: `decide` fails with `ContentPolicyError`, and `decideSettled` returns a `Refused` outcome for just that decision (`OpenAiContentPolicyMetadata` and `metadata.openai.refusedDecisions` are gone). Images are labelled with their reference. Images must be inline bytes, base64, or data URLs; remote image URLs fail with `InvalidUserInputError` before dispatch. The provider tolerates two-decimal probability rounding.

- **pkg @effect/ai-cloudflare** — Cloudflare Workers AI decision provider. `CloudflareClient.layerConfig()` reads `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`; `CloudflareDecisionModel.layer({ model: "clef-flash" })` (or `"clef"` for the larger model) provides `DecisionModel`. No `LanguageModel` — decisions only. Limits: 64 questions per request, 2–255 classification choices, 2–10 rating levels. Rate-limit errors carry `Retry-After` when provided; the client validates answers and rescales small probability drift from four-decimal rounding. Set `gatewayId` (or `CLOUDFLARE_AI_GATEWAY_ID`) to route through Cloudflare AI Gateway via the `cf-aig-gateway-id` header.

```ts
// Swapping providers is a one-line change at the edge — comp logic is untouched.
import { Config, Layer } from "effect"
import { FetchHttpClient } from "effect/http"
import { AmazonBedrockClient, AmazonBedrockLanguageModel } from "@effect/ai-amazon-bedrock"
import { AnthropicClient, AnthropicLanguageModel } from "@effect/ai-anthropic"
import { OpenRouterClient, OpenRouterLanguageModel } from "@effect/ai-openrouter"
import { Digest, Mac, SecureRandom } from "effect/crypto"
import { TypeSafeClient, TypeSafeDecisionModel } from "@effect/ai-typesafe"

const AnthropicLive = AnthropicLanguageModel.model("claude-opus-4-6").pipe(
  Layer.provide(
    AnthropicClient.layerConfig({ apiKey: Config.Redacted("ANTHROPIC_API_KEY") })
      .pipe(Layer.provide(FetchHttpClient.layer))
  )
)

const OpenRouterLive = OpenRouterLanguageModel.model("openai/gpt-5.2").pipe(
  Layer.provide(
    OpenRouterClient.layerConfig({
      apiKey: Config.Redacted("OPENROUTER_API_KEY"),
      siteTitle: Config.succeed("Comp Planner")
    }).pipe(Layer.provide(FetchHttpClient.layer))
  )
)

// Bedrock reads AWS_REGION plus AWS keys or AWS_BEARER_TOKEN_BEDROCK, and signs
// requests with the effect/crypto Digest and Mac services.
const BedrockLive = AmazonBedrockLanguageModel.model("us.anthropic.claude-sonnet-4-5-20250929-v1:0").pipe(
  Layer.provide(AmazonBedrockClient.layerConfig()),
  Layer.provide([
    FetchHttpClient.layer,
    Digest.layerWebCrypto,
    Mac.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto))
  ])
)

// A DecisionModel provider is wired the same way; it reads TYPESAFE_API_KEY.
const TypeSafeDecisionsLive = TypeSafeDecisionModel.model("jev-latest").pipe(
  Layer.provide(TypeSafeClient.layerConfig().pipe(Layer.provide(FetchHttpClient.layer)))
)
```

> **Note:** Configure a provider client from `Config` → write comp logic against `LanguageModel` → call `generateText` for a review summary, `generateObject` with a `Schema` for a validated `RaiseRecommendation`, and pass a `Toolkit` (e.g. `LookupCompBand`) for tool calls → provide a concrete `.model(...)` Layer at the edge. Same four moves whether you're on OpenAI, Anthropic, Amazon Bedrock, OpenRouter, or a local model.
