Skip to content

Commit d3980d7

Browse files
committed
[AI Gateway] Add workers-ai-provider example for calling third-party models via gateway
1 parent 3d4950e commit d3980d7

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/content/docs/ai-gateway/integrations/vercel-ai-sdk.mdx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,34 @@ npm install ai-gateway-provider
2121

2222
<CodeSnippets forceClient="aisdk" />
2323

24-
### Fallback Providers
24+
### Workers AI binding with third-party models
25+
26+
If you are already using the [`workers-ai-provider`](https://www.npmjs.com/package/workers-ai-provider) package, you can route requests through AI Gateway to call third-party models without needing separate provider SDKs. Pass a `gateway` option with your gateway ID to `createWorkersAI`:
27+
28+
```ts
29+
import { createWorkersAI } from "workers-ai-provider";
30+
import { streamText } from "ai";
31+
32+
export default {
33+
async fetch(request, env) {
34+
const workersai = createWorkersAI({
35+
binding: env.AI,
36+
gateway: { id: "my-gateway" },
37+
});
38+
39+
const result = streamText({
40+
model: workersai("openai/gpt-4o"),
41+
messages: [{ role: "user", content: "Write a short story" }],
42+
});
43+
44+
return result.toTextStreamResponse();
45+
},
46+
} satisfies ExportedHandler<Env>;
47+
```
48+
49+
This works with any [supported provider and model](/ai-gateway/providers/) available through AI Gateway.
50+
51+
### Fallback providers
2552

2653
To specify model or provider fallbacks to handle request failures and ensure reliability, you can pass an array of models to the `model` option.
2754

0 commit comments

Comments
 (0)