1---
2title: AI Gateway setup
3description: Buy Neon AI Gateway credits and use a model from an Effect Function.
4---
5
6Use Neon AI Gateway from an Effect Function with a model your organization can
7access. Complete [Setup](/neon/setup) first.
8
9## Buy AI Gateway credits
10
11[Buy AI Gateway credits in the Neon Console](https://console.neon.tech/app/billing)
12before sending model requests. AI Gateway requires a paid Neon plan; follow
13[Neon's purchase guide](https://neon.com/docs/ai-gateway/prepaid-credits) for the
14steps and current requirements.
15
16Choose a Chat Completions-compatible model from the
17[model catalog](https://neon.com/docs/ai-gateway/models) that your organization
18can access. The examples use the supported `aws-us-east-2` region.
19
20## Declare the branch gateway
21
22Inside your stack, using an existing `branch`:
23
24```typescript
25import * as Neon from "alchemy/Neon";
26
27const gateway = yield* Neon.AIGateway("AI", { branch });
28```
29
30This discovers the endpoint without sending an inference request or changing
31billing. See the [resource reference](/providers/neon/aigateway) for explicit
32project/branch IDs and an optional credential reference.
33
34## Bind an Effect model
35
36Inside a Function's initialization Effect:
37
38```typescript
39const ai = yield* Neon.QueryAIGateway(gateway);
40```
41
42Provide `Neon.QueryAIGatewayHttp` on the application. It uses the injected
43credential for a same-branch production Neon Function and manages a tracked
44`ai_gateway:invoke` credential for a Worker, Lambda, local Function, or cross-branch
45caller. An explicit credential on `Neon.AIGateway` overrides automatic injection.
46Every path uses HTTP; Neon does not expose a Cloudflare-style native gateway
47binding. The deployment management key is never the inference credential.
48
49## Verify one small request
50
51Inside an authenticated request handler, use a Chat Completions model ID your
52organization can access. The following handler excerpt assumes `modelId` is that
53configured string:
54
55```typescript
56import * as Effect from "effect/Effect";
57import { LanguageModel } from "effect/ai";
58
59const reply = yield* LanguageModel.generateText({
60 prompt: "Reply with hello.",
61}).pipe(
62 Effect.provide(ai.model({
63 model: modelId,
64 parameters: { maxTokens: 16 },
65 })),
66);
67```
68
69Call the deployed route once, confirm it returns model text, and check the
70organization's usage. The request consumes credits; it is not a deployment-time
71probe. Some models use `maxCompletionTokens` or need a larger reasoning budget;
72use the catalog's supported settings rather than sending both token-limit fields.
73Protect the route with authentication and rate limits before exposing it to users.
74
75After basic inference works, exercise the actual features you need: streamed
76output, tools or structured results, and cancelling a stream. Endpoint discovery
77alone does not verify any of these behaviors. See
78[AI Gateway bindings](/providers/neon/queryaigateway) and the
79[Effect model layer](/providers/neon/makelanguagemodellayer).
80
81## Diagnose access failures
82
83| Symptom | Check | Action |
84| --- | --- | --- |
85| `403` with `ai gateway not enabled for account` | Correct organization, paid-plan access, and account enablement | Resolve access in the Console or with Neon Support before retrying; redeploying the Function does not enable the account |
86| Model access denied | Approval for that model, not just gateway access | Select an accessible model or request foundation-model access |
87| Authentication failure | Credential scope and target branch | Use an `ai_gateway:invoke` service credential or the same-branch Function's injected credential, not a management API key |
88| `429` / `REQUEST_LIMIT_EXCEEDED` | Token rate, daily cap, and credit balance | Inspect rate-limit headers and Billing; distinguish transient rate limiting from a quota requiring operator action |
89| Incompatible request or model | Chat Completions versus Responses/Anthropic API | Match the model to the adapter or use the appropriate bound SDK URL |
90
91Preserve request IDs and sanitized errors when contacting
92[Neon Support](https://neon.com/docs/introduction/support). Exclude bearer tokens,
93private prompts, and response data from public reports. Neon's
94[troubleshooting guide](https://neon.com/docs/ai-gateway/troubleshooting) covers
95current service limits.
96
97PrivateLink and IP Allow do not cover AI Gateway traffic. A private database
98connection does not make this model endpoint private.
99
100## Related guides
101
102- [Setup](/neon/setup) — management credentials and feature prerequisites.
103- [Private networking](/neon/guides/private-networking) — database-only network controls.
104- [State and recovery](/neon/guides/state-recovery) — retain managed credential state.