LanguageModel and DecisionModel services backed by the Convex AI gateway. Each client obtains a short-lived credential from the running Convex action before each outgoing request, so you do not need to configure or rotate an upstream model-provider API key.
The AI gateway requires a paid Convex plan. See gateway availability for supported deployment types and local-development requirements.
Generate text
Provide three layers to an Effect AI operation:AiGatewayLanguageModel.model(...)selects a gateway model using itsprovider/modelidentifier.AiGatewayLanguageClient.layerauthenticates requests with the current Convex deployment.FetchHttpClient.layersends requests through the Fetch API available in both Convex action runtimes.
confect/assistant.impl.ts
Configure requests
Pass request options as the second argument tomodel:
AiGatewayLanguageModel.withConfigOverride when configuration should apply only within part of a larger Effect.
Stream responses
LanguageModel.streamText consumes the gateway’s server-sent event stream inside the action. A normal Convex action still returns one value to its caller; use a Convex HTTP action or persist partial output when a client must observe tokens as they arrive.
Handle errors
Handle recoverable failures after providing the client layer:Effect.orDie when your action declares a recoverable error schema.
The client checks gateway availability during construction, when AiGatewayDisabled and AiGatewayUnavailable can occur. If obtaining a token fails for a later request, model operations report an Effect AI AiError with a NetworkError reason; direct HTTP client calls report an HttpClientError whose transport-error cause preserves the gateway error. The failed request is not sent.
Make structured decisions
UseAiGatewayDecisionModel with Effect’s DecisionModel.decide to classify input, rate it against an ordered rubric, or estimate a probability. Unlike a language model, a decision model returns typed answers rather than generated text.
Define the input schema and named decisions with Effect’s Decision module, then provide a decision model and its client:
triage inside a Convex action. All three decisions evaluate the same input in one request.
Use AiGatewayDecisionClient.layer for decision models and AiGatewayLanguageClient.layer for language models. Use AiGatewayDecisionModel.make({ model }) to construct the service directly, or AiGatewayDecisionModel.layer({ model }) when you do not need an Effect AI model descriptor. Unlike language-model requests, decision requests do not expose configuration overrides.
Classifications return a label and a probability distribution. Ratings return a numeric rating, the label with the highest probability, and a distribution keyed by rubric labels. Probability decisions return a number between zero and one. The response also includes usage.inputTokens and usage.outputTokens.