Open History to see the last commit
1---
2id: keyedAdapters
3title: keyedAdapters
4---
5
6```ts
7function keyedAdapters<TMap>(map): { [K in string | number | symbol]: TMap[K] extends KeyedAdapter<A> ? KeyedAdapter<A> : TMap[K] extends (key: string) => A ? KeyedAdapter<A> : never };
8```
9
10Defined in: [packages/ai/src/byok/keyed.ts:113](https://github.com/TanStack/ai/blob/main/packages/ai/src/byok/keyed.ts#L113)
11
12Keyed adapters for several providers in one map. The map key is the
13provider id, so a key goes only to the factory of its own provider. An
14entry is a factory (the key comes only from the `x-byok-<id>` header), or a
15[keyedAdapter](keyedAdapter.md) made with a BYOK descriptor (its `env` names are the
16fallback). Pick one per request with `keyedAdapterFromRequest` from
17`@tanstack/ai/byok/server`.
18
19## Type Parameters
20
21### TMap
22
23`TMap` *extends* `Record`\<`string`,
24 \| [`KeyedAdapter`](../interfaces/KeyedAdapter.md)\<`unknown`\>
25 \| ((`key`) => `unknown`)\>
26
27## Parameters
28
29### map
30
31`TMap`
32
33## Returns
34
35\{ \[K in string \| number \| symbol\]: TMap\[K\] extends KeyedAdapter\<A\> ? KeyedAdapter\<A\> : TMap\[K\] extends (key: string) =\> A ? KeyedAdapter\<A\> : never \}
36
37## Throws
38
39Error when a key is not a valid provider id, or when a keyed adapter
40 entry is for a different provider than its key.
41
42## Example
43
44```ts
45const models = keyedAdapters({
46 openai: (key) => createOpenaiChat('gpt-6.1-sol', key),
47 anthropic: keyedAdapter(anthropicByok, (key) =>
48 createAnthropicChat('claude-sonnet-5-5', key),
49 ),
50})
51```