forked from adamstankiewicz/interactive-learning-experiences
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.ts
More file actions
59 lines (51 loc) · 2.3 KB
/
Copy pathmodel.ts
File metadata and controls
59 lines (51 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';
import { anthropic } from '@ai-sdk/anthropic';
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
import type { LanguageModel } from 'ai';
/**
* One place to resolve which Claude to talk to.
*
* LLM_PROVIDER=anthropic (default) — first-party Anthropic API.
* LLM_PROVIDER=openrouter — OpenRouter gateway. One key, no cloud setup.
* LLM_PROVIDER=bedrock — Claude on Amazon Bedrock.
*
* Each provider names the same model differently, hence the three defaults:
* anthropic claude-opus-5
* openrouter anthropic/claude-opus-5
* bedrock us.anthropic.claude-opus-5
*/
const DEFAULTS = {
anthropic: 'claude-opus-5',
openrouter: 'anthropic/claude-opus-5',
// Bedrock access is granted per model per account, and Opus-tier is commonly
// not enabled — Sonnet is the safer default. Override with BEDROCK_MODEL_ID.
bedrock: 'us.anthropic.claude-sonnet-4-5-20250929-v1:0',
} as const;
export function pathwayModel(): LanguageModel {
const provider = (process.env.LLM_PROVIDER ?? 'anthropic').toLowerCase();
if (provider === 'openrouter') {
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) {
throw new Error('LLM_PROVIDER=openrouter but OPENROUTER_API_KEY is not set.');
}
const openrouter = createOpenRouter({ apiKey });
return openrouter.chat(process.env.OPENROUTER_MODEL_ID ?? DEFAULTS.openrouter);
}
if (provider === 'bedrock') {
// Bedrock model ids carry an `anthropic.` prefix, and most newer models are
// served on-demand only through a cross-region inference profile, which adds
// a geography prefix on top (`us.anthropic....`). An "on-demand throughput
// isn't supported" error means the bare id needs that `us.` prefix.
//
// apiKey maps to a Bedrock API key (bearer token). Omit it and the provider
// falls back to standard SigV4 credential resolution.
const bedrock = createAmazonBedrock({
region: process.env.AWS_REGION ?? 'us-west-2',
...(process.env.AWS_BEARER_TOKEN_BEDROCK
? { apiKey: process.env.AWS_BEARER_TOKEN_BEDROCK }
: {}),
});
return bedrock(process.env.BEDROCK_MODEL_ID ?? DEFAULTS.bedrock);
}
return anthropic(process.env.ANTHROPIC_MODEL_ID ?? DEFAULTS.anthropic);
}