Skip to main content
Confect provides Effect AI 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 its provider/model identifier.
  • AiGatewayLanguageClient.layer authenticates requests with the current Convex deployment.
  • FetchHttpClient.layer sends requests through the Fetch API available in both Convex action runtimes.
confect/assistant.impl.ts
Use the same model with Effect AI tools and structured responses. The selected upstream model determines which capabilities are available.

Configure requests

Pass request options as the second argument to model:
Use 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:
Handle recoverable failures before calling 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

Use AiGatewayDecisionModel 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:
Run 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.