Ai
Working with AI modules
Section titled “Working with AI modules”Effect’s AI modules provide a provider-agnostic interface for language models.
You can generate text, decode structured objects with Schema and stream partial
responses.
Using LanguageModel for text, objects, and streams
Section titled “Using LanguageModel for text, objects, and streams”Configure a provider once, then use LanguageModel for plain text
generation, schema-validated object generation, and streaming responses.
import { AnthropicClient, AnthropicLanguageModel } from "@effect/ai-anthropic"import { OpenAiClient, OpenAiLanguageModel } from "@effect/ai-openai"import { Config, Context, Effect, ExecutionPlan, Layer, Schema, Stream } from "effect"import { AiError, LanguageModel, Model, type Response } from "effect/unstable/ai"import { FetchHttpClient } from "effect/unstable/http"import { LaunchPlan } from "./fixtures/domain/LaunchPlan.ts"
// You can use Config to create ai clientsconst AnthropicClientLayer = AnthropicClient.layerConfig({ apiKey: Config.redacted("ANTHROPIC_API_KEY")}).pipe( // Providers typically require an HttpClient, but you can choose which one to // use. Layer.provide(FetchHttpClient.layer))
const OpenAiClientLayer = OpenAiClient.layerConfig({ apiKey: Config.redacted("OPENAI_API_KEY")}).pipe( Layer.provide(FetchHttpClient.layer))
export class AiWriterError extends Schema.TaggedErrorClass<AiWriterError>()("AiWriterError", { // AiErrorReason is a Schema, so we can include it directly in our custom // error schema. reason: AiError.AiErrorReason}) { static fromAiError(error: AiError.AiError) { return new AiWriterError({ reason: error.reason }) }}
// You can use `ExecutionPlan` to define a strategy for trying multiple// providers with different configurations. In this example, we try a cheaper// OpenAI model first, then fall back to a more expensive Anthropic model if the// first one fails.const DraftPlan = ExecutionPlan.make( { provide: OpenAiLanguageModel.model("gpt-5.2"), // Attempt to use the openai model up to 3 times before falling back to the // anthropic model. attempts: 3 }, { provide: AnthropicLanguageModel.model("claude-opus-4-6"), attempts: 2 })
export class AiWriter extends Context.Service<AiWriter, { draftAnnouncement(product: string): Effect.Effect<{ readonly provider: string readonly text: string }, AiWriterError> extractLaunchPlan(notes: string): Effect.Effect<LaunchPlan, AiWriterError> streamReleaseHighlights(version: string): Stream.Stream<string, AiWriterError>}>()("docs/AiWriter") { static readonly layer = Layer.effect( AiWriter, Effect.gen(function*() { // Calling `captureRequirements` on an `ExecutionPlan` will move the // requirements of the plan (in this case the ai clients) into the Layer // requirements. const draftsModel = yield* DraftPlan.captureRequirements
// Use a different model for the launch plan extraction const launchPlanModel = yield* OpenAiLanguageModel.model("gpt-4.1").captureRequirements
const draftAnnouncement = Effect.fn("AiWriter.draftAnnouncement")( function*(product: string) { const model = yield* LanguageModel.LanguageModel const provider = yield* Model.ProviderName const response = yield* model.generateText({ prompt: `Write a short launch announcement for ${product}. ` + "Keep it concise and include one concrete user benefit." })
// `LanguageModel.generateText` exposes convenience fields so you can // inspect usage and finish reason without parsing content parts. yield* Effect.logInfo( `${provider} finished with ${response.finishReason}. outputTokens=${response.usage.outputTokens.total}` )
return { provider, text: response.text } }, // To apply an `ExecutionPlan`, we use `Effect.withExecutionPlan` Effect.withExecutionPlan(draftsModel), // Map AiError into our custom error type Effect.mapError((error) => AiWriterError.fromAiError(error)) )
const extractLaunchPlan = Effect.fn("AiWriter.extractLaunchPlan")( function*(notes: string) { const model = yield* LanguageModel.LanguageModel const response = yield* model.generateObject({ objectName: "launch_plan", prompt: "Convert these notes into a launch plan object with audience, channels, launchDate, summary, and keyRisks:\n" + notes, // The generated object is validated and decoded through this schema. schema: LaunchPlan })
return response.value }, // The .model(...) apis return a Layer that can be used with // Effect.provide Effect.provide(launchPlanModel), // Map AiError into our custom error type Effect.mapError((error) => AiWriterError.fromAiError(error)) )
const streamReleaseHighlights = (version: string) => LanguageModel.streamText({ prompt: `Write release highlights for version ${version} as a short bulleted list.` }).pipe( Stream.filter((part): part is Response.TextDeltaPart => part.type === "text-delta"), Stream.map((part) => part.delta), Stream.provide(launchPlanModel), // Map AiError into our custom error type Stream.mapError((error) => AiWriterError.fromAiError(error)) )
return AiWriter.of({ draftAnnouncement, extractLaunchPlan, streamReleaseHighlights }) }) ).pipe( // This Layer has requirements for both the OpenAI and Anthropic clients, // since the ExecutionPlan includes models from both providers. Layer.provide([OpenAiClientLayer, AnthropicClientLayer]) )}
// We can now use `AiWriter` like any other Effect service.export const program: Effect.Effect< void, AiWriterError, AiWriter> = Effect.gen(function*() { const writer = yield* AiWriter yield* writer.draftAnnouncement("Effect Cloud")})Defining and using AI tools
Section titled “Defining and using AI tools”Define tools with schemas, group them into toolkits, implement handlers,
and pass them to LanguageModel.generateText.
import { OpenAiClient, OpenAiLanguageModel, OpenAiTool } from "@effect/ai-openai"import { Config, Context, Effect, Layer, Schema } from "effect"import { AiError, LanguageModel, Tool, Toolkit } from "effect/unstable/ai"import { FetchHttpClient } from "effect/unstable/http"
// ---------------------------------------------------------------------------// 1. Defining tools// ---------------------------------------------------------------------------
const ProductId = Schema.String.pipe(Schema.brand("ProductId")).annotate({ description: "A unique identifier for a product, e.g. 'p-123'"})
class Product extends Schema.Class<Product>("acme/domain/Product")({ id: ProductId, name: Schema.String, price: Schema.Number}) {}
// Each tool has a name, an optional description, a parameters schema that the// model fills in, and a success schema for the handler result. The description// is shown to the model to help it decide when to call the tool.const SearchProducts = Tool.make("SearchProducts", { description: "Search the product catalog by keyword", parameters: Schema.Struct({ query: Schema.String.annotate({ // Add a description to individual parameters for even better model // guidance. description: "The search query, e.g. 'wireless headphones'" }), maxResults: Schema.Number.pipe(Schema.withDecodingDefault(Effect.succeed(10))).annotate({ description: "The maximum number of results to return" }) }), success: Schema.Array(Product), // The strategy used for handling errors returned from tool call handler // execution. // // If set to `"error"` (the default), errors that occur during tool call handler // execution will be returned in the error channel of the calling effect. // // If set to `"return"`, errors that occur during tool call handler execution // will be captured and returned as part of the tool call result. failureMode: "error"})
const GetInventory = Tool.make("GetInventory", { description: "Check current stock level for a product", parameters: Schema.Struct({ productId: ProductId }), success: Schema.Struct({ productId: ProductId, available: Schema.Number })})
// ---------------------------------------------------------------------------// 2. Grouping tools into a Toolkit// ---------------------------------------------------------------------------
// `Toolkit.make` accepts any number of tools and produces a typed toolkit that// knows the names and schemas of every tool it contains.const ProductToolkit = Toolkit.make(SearchProducts, GetInventory)
// ---------------------------------------------------------------------------// 3. Implementing handlers via toLayer// ---------------------------------------------------------------------------
// `toLayer` returns a `Layer` that satisfies the handler requirements for every// tool in the toolkit. Each handler receives the decoded parameters and returns// an Effect producing the success type.const ProductToolkitLayer = ProductToolkit.toLayer(Effect.gen(function*() { yield* Effect.log("Initializing ProductToolkitLive") // Here you could access other services or resources needed to implement the // handlers, e.g. a database client or external API client. // // const client = yield* SomeDatabaseClient return ProductToolkit.of({ SearchProducts: Effect.fn("ProductToolkit.SearchProducts")(function*({ query, maxResults }) { return [ new Product({ id: ProductId.make("p-1"), name: `${query} widget`, price: 19.99 }), new Product({ id: ProductId.make("p-2"), name: `${query} gadget`, price: 29.99 }) ].slice(0, maxResults) }), GetInventory: Effect.fn("ProductToolkit.GetInventory")(function*({ productId }) { return { productId, available: 42 } }) })}))
// ---------------------------------------------------------------------------// 4. Using tools with LanguageModel// ---------------------------------------------------------------------------
// Provider setup (same pattern as the language-model example).const OpenAiClientLayer = OpenAiClient.layerConfig({ apiKey: Config.redacted("OPENAI_API_KEY")}).pipe(Layer.provide(FetchHttpClient.layer))
export class ProductAssistantError extends Schema.TaggedErrorClass<ProductAssistantError>()( "ProductAssistantError", { reason: AiError.AiErrorReason }) {}
// Wrap tool-enabled generation in a serviceexport class ProductAssistant extends Context.Service<ProductAssistant, { answer(question: string): Effect.Effect<{ readonly text: string readonly toolCallCount: number }, ProductAssistantError>}>()("docs/ProductAssistant") { static readonly layer = Layer.effect( ProductAssistant, Effect.gen(function*() { // Access the toolkit's handlers by yielding the toolkit definition. const toolkit = yield* ProductToolkit
// Choose a model to use const model = yield* OpenAiLanguageModel.model("gpt-5.2").captureRequirements
const answer = Effect.fn("ProductAssistant.answer")( function*(question: string) { // Pass the toolkit to `generateText`. The model can call any tool in // the toolkit; the framework resolves parameters, invokes handlers, // and feeds results back automatically. const response = yield* LanguageModel.generateText({ prompt: question, toolkit, // You can set `toolChoice` to "required" to force the model to call // a tool before responding with text. // // By default it is set to "auto" toolChoice: "required" })
// ------------------------------------------------------------------- // 5. Inspecting tool calls and results // -------------------------------------------------------------------
// `response.toolCalls` lists every tool the model invoked, each with // the tool name, a unique id, and the decoded parameters. for (const call of response.toolCalls) { yield* Effect.log(`Tool call: ${call.name} id=${call.id}`) }
// `response.toolResults` lists the resolved results, each with the // tool name, id, decoded result, and an `isFailure` flag. for (const result of response.toolResults) { yield* Effect.log( `Tool result: ${result.name} id=${result.id} isFailure=${result.isFailure}` ) }
return { text: response.text, toolCallCount: response.toolCalls.length } }, // Provide the chosen model to use Effect.provide(model), (_) => _, // Map AI errors into our domain error type Effect.catchTag( "AiError", (error) => Effect.fail( new ProductAssistantError({ reason: error.reason }) ), // For unexpected errors, die with the original error (e) => Effect.die(e) ) )
return ProductAssistant.of({ answer }) }) ).pipe( // The toolkit handler layer must be provided so the framework can invoke // the tool handlers when the model makes tool calls. Layer.provide(ProductToolkitLayer), // Also provide the openai client required by OpenAiLanguageModel.model Layer.provide(OpenAiClientLayer) )}
// ---------------------------------------------------------------------------// 6. Provider-defined tools// ---------------------------------------------------------------------------
// Some providers offer built-in tools (web search, code interpreter, etc.)// that run server-side. Use `Tool.providerDefined` or the pre-built// definitions from provider packages.
// OpenAI's web search tool is pre-defined in `@effect/ai-openai`. Calling it// produces a tool instance that can be merged into any toolkit.const webSearch = OpenAiTool.WebSearch({ search_context_size: "medium"})
// Combine user-defined and provider-defined tools in a single toolkit.const AssistantToolkit = Toolkit.make(SearchProducts, GetInventory, webSearch)
// Only user-defined tools that require handlers appear in `toLayer`. The// provider-defined `WebSearch` is executed server-side by the provider.export const AssistantToolkitLayer = AssistantToolkit.toLayer(Effect.gen(function*() { yield* Effect.log("Initializing AssistantToolkitLive") return AssistantToolkit.of({ SearchProducts: Effect.fn("AssistantToolkit.SearchProducts")(function*({ query, maxResults }) { return [ new Product({ id: ProductId.make("p-1"), name: `${query} widget`, price: 19.99 }), new Product({ id: ProductId.make("p-2"), name: `${query} gadget`, price: 29.99 }) ].slice(0, maxResults) }), GetInventory: Effect.fn("AssistantToolkit.GetInventory")(function*({ productId }) { return { productId, available: 42 } }) })}))Stateful chat sessions
Section titled “Stateful chat sessions”The AI Chat module maintains conversation history automatically. Build
AI agents or chat assistants.
import { OpenAiClient, OpenAiLanguageModel } from "@effect/ai-openai"import { Config, Context, DateTime, Effect, Layer, Ref, Schema } from "effect"import { AiError, Chat, Prompt, Tool, Toolkit } from "effect/unstable/ai"import { FetchHttpClient } from "effect/unstable/http"
// ---------------------------------------------------------------------------// Provider setup// ---------------------------------------------------------------------------
const OpenAiClientLayer = OpenAiClient.layerConfig({ apiKey: Config.redacted("OPENAI_API_KEY")}).pipe(Layer.provide(FetchHttpClient.layer))
// ---------------------------------------------------------------------------// Tools for the agentic loop// ---------------------------------------------------------------------------
const Tools = Toolkit.make(Tool.make("getCurrentTime", { description: "Get the current time in ISO format", parameters: Schema.Struct({ id: Schema.String }), success: Schema.String}))
const ToolsLayer = Tools.toLayer(Effect.gen(function*() { yield* Effect.logDebug("Initializing tools...") return Tools.of({ getCurrentTime: Effect.fn("Tools.getCurrentTime")(function*(_) { const now = yield* DateTime.now return DateTime.formatIso(now) }) })}))
// ---------------------------------------------------------------------------// Service that wraps Chat for a domain use-case// ---------------------------------------------------------------------------
export class AiAssistantError extends Schema.TaggedErrorClass<AiAssistantError>()("AiAssistantError", { reason: AiError.AiErrorReason}) { static fromAiError(error: AiError.AiError) { return new AiAssistantError({ reason: error.reason }) }}
export class AiAssistant extends Context.Service<AiAssistant, { // Send a message while maintaining conversation history across turns. chat(message: string): Effect.Effect<string, AiAssistantError> // Ask a question and use an agentic loop with tool calls to answer it. agent(question: string): Effect.Effect<string, AiAssistantError>}>()("acme/AiAssistant") { static readonly layer = Layer.effect( AiAssistant, Effect.gen(function*() { // Choose the model you want to use for the chat sessions. const modelLayer = yield* OpenAiLanguageModel.model("gpt-5.2").captureRequirements
// --------------------------------------------------------------------------- // 1. Chat.empty — basic multi-turn conversation // ---------------------------------------------------------------------------
// Create a new chat session with `Chat.empty` or `Chat.fromPrompt`. The // session maintains conversation history automatically, so you can focus on // the current turn without having to manage context. const newSession = yield* Chat.fromPrompt(Prompt.empty.pipe( Prompt.setSystem("You are a helpful assistant that answers questions.") ))
// You can also create a chat using a json export. const json = yield* newSession.exportJson const session = yield* Chat.fromJson(json)
const chat = Effect.fn("AiAssistant.chat")( function*(message: string) { // Create a new turn in the conversation by passing the user's message // to `session.generateText`. const response = yield* session.generateText({ prompt: message }).pipe( // Provide the model layer to use. // You could potentially use different models for different turns, // or even switch models in the middle of a conversation. Effect.provide(modelLayer) )
// You can inspect the accumulated history at any point through the // `history` ref on the chat instance. const history = yield* Ref.get(session.history) yield* Effect.logInfo( `Conversation has ${history.content.length} messages` )
return response.text }, Effect.mapError((error) => AiAssistantError.fromAiError(error)) )
// --------------------------------------------------------------------------- // 2. Create agentic loops with tools // ---------------------------------------------------------------------------
const tools = yield* Tools const agent = Effect.fn("AiAssistant.agent")( function*(question: string) { // We start the agent with a system prompt and the user question. The // agent can then call tools in a loop until it decides to return a // final answer. const session = yield* Chat.fromPrompt([ { role: "system", content: "You are an assistant that can use tools to answer questions." }, { role: "user", content: question } ])
while (true) { const response = yield* session.generateText({ prompt: [], // No additional prompt — the model has full access to the conversation history toolkit: tools // Provide the tools to the model }).pipe( // Provide the model layer to use. // You could potentially use different models for different turns, // or even switch models in the middle of a conversation. Effect.provide(modelLayer) ) if (response.toolCalls.length > 0) { // If the model called any tools, execute them and the Chat module // will automatically add the tool results to the conversation // history before the next turn. continue } // If there are no tool calls, the model has returned a final answer // and we can exit the loop. return response.text } }, // Remap AI errors to our domain-specific error type, but die on // unexpected errors. Effect.catchTag( "AiError", (error) => Effect.fail(AiAssistantError.fromAiError(error)), (e) => Effect.die(e) ) )
return AiAssistant.of({ chat, agent }) }) ).pipe( // Provide the OpenAI client and tools layers to the AiAssistant service. Layer.provide([OpenAiClientLayer, ToolsLayer]) )}