Skip to content

Commit e3710b6

Browse files
committed
Add providerOptions to agent definition
1 parent ee5c6c1 commit e3710b6

File tree

7 files changed

+270
-26
lines changed

7 files changed

+270
-26
lines changed

.agents/types/agent-definition.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,66 @@ export interface AgentDefinition {
5151
}
5252
)
5353

54+
/**
55+
* Provider routing options for OpenRouter.
56+
* Controls which providers to use and fallback behavior.
57+
* See https://openrouter.ai/docs/features/provider-routing
58+
*/
59+
providerOptions?: {
60+
/**
61+
* List of provider slugs to try in order (e.g. ["anthropic", "openai"])
62+
*/
63+
order?: string[]
64+
/**
65+
* Whether to allow backup providers when primary is unavailable (default: true)
66+
*/
67+
allow_fallbacks?: boolean
68+
/**
69+
* Only use providers that support all parameters in your request (default: false)
70+
*/
71+
require_parameters?: boolean
72+
/**
73+
* Control whether to use providers that may store data
74+
*/
75+
data_collection?: 'allow' | 'deny'
76+
/**
77+
* List of provider slugs to allow for this request
78+
*/
79+
only?: string[]
80+
/**
81+
* List of provider slugs to skip for this request
82+
*/
83+
ignore?: string[]
84+
/**
85+
* List of quantization levels to filter by (e.g. ["int4", "int8"])
86+
*/
87+
quantizations?: Array<
88+
| 'int4'
89+
| 'int8'
90+
| 'fp4'
91+
| 'fp6'
92+
| 'fp8'
93+
| 'fp16'
94+
| 'bf16'
95+
| 'fp32'
96+
| 'unknown'
97+
>
98+
/**
99+
* Sort providers by price, throughput, or latency
100+
*/
101+
sort?: 'price' | 'throughput' | 'latency'
102+
/**
103+
* Maximum pricing you want to pay for this request
104+
*/
105+
max_price?: {
106+
prompt?: number | string
107+
completion?: number | string
108+
image?: number | string
109+
audio?: number | string
110+
request?: number | string
111+
}
112+
}
113+
54114
// ============================================================================
55115
// Tools and Subagents
56116
// ============================================================================

common/src/templates/initial-agents-dir/types/agent-definition.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,66 @@ export interface AgentDefinition {
5151
}
5252
)
5353

54+
/**
55+
* Provider routing options for OpenRouter.
56+
* Controls which providers to use and fallback behavior.
57+
* See https://openrouter.ai/docs/features/provider-routing
58+
*/
59+
providerOptions?: {
60+
/**
61+
* List of provider slugs to try in order (e.g. ["anthropic", "openai"])
62+
*/
63+
order?: string[]
64+
/**
65+
* Whether to allow backup providers when primary is unavailable (default: true)
66+
*/
67+
allow_fallbacks?: boolean
68+
/**
69+
* Only use providers that support all parameters in your request (default: false)
70+
*/
71+
require_parameters?: boolean
72+
/**
73+
* Control whether to use providers that may store data
74+
*/
75+
data_collection?: 'allow' | 'deny'
76+
/**
77+
* List of provider slugs to allow for this request
78+
*/
79+
only?: string[]
80+
/**
81+
* List of provider slugs to skip for this request
82+
*/
83+
ignore?: string[]
84+
/**
85+
* List of quantization levels to filter by (e.g. ["int4", "int8"])
86+
*/
87+
quantizations?: Array<
88+
| 'int4'
89+
| 'int8'
90+
| 'fp4'
91+
| 'fp6'
92+
| 'fp8'
93+
| 'fp16'
94+
| 'bf16'
95+
| 'fp32'
96+
| 'unknown'
97+
>
98+
/**
99+
* Sort providers by price, throughput, or latency
100+
*/
101+
sort?: 'price' | 'throughput' | 'latency'
102+
/**
103+
* Maximum pricing you want to pay for this request
104+
*/
105+
max_price?: {
106+
prompt?: number | string
107+
completion?: number | string
108+
image?: number | string
109+
audio?: number | string
110+
request?: number | string
111+
}
112+
}
113+
54114
// ============================================================================
55115
// Tools and Subagents
56116
// ============================================================================

common/src/types/agent-template.ts

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,81 @@ import type { z } from 'zod/v4'
1919

2020
export type AgentId = `${string}/${string}@${number}.${number}.${number}`
2121

22-
export type OpenRouterProviderOptions = {
23-
models?: string[]
24-
22+
export type OpenRouterReasoningOptions = {
2523
/**
2624
* https://openrouter.ai/docs/use-cases/reasoning-tokens
2725
* One of `max_tokens` or `effort` is required.
2826
* If `exclude` is true, reasoning will be removed from the response. Default is false.
2927
*/
30-
reasoning?: {
31-
enabled?: boolean
32-
exclude?: boolean
33-
} & (
34-
| {
35-
max_tokens: number
36-
}
37-
| {
38-
effort: 'high' | 'medium' | 'low'
39-
}
40-
)
28+
enabled?: boolean
29+
exclude?: boolean
30+
} & (
31+
| {
32+
max_tokens: number
33+
}
34+
| {
35+
effort: 'high' | 'medium' | 'low'
36+
}
37+
)
38+
39+
export type OpenRouterProviderRoutingOptions = {
40+
/**
41+
* List of provider slugs to try in order (e.g. ["anthropic", "openai"])
42+
*/
43+
order?: string[]
44+
/**
45+
* Whether to allow backup providers when primary is unavailable (default: true)
46+
*/
47+
allow_fallbacks?: boolean
48+
/**
49+
* Only use providers that support all parameters in your request (default: false)
50+
*/
51+
require_parameters?: boolean
52+
/**
53+
* Control whether to use providers that may store data
54+
*/
55+
data_collection?: 'allow' | 'deny'
56+
/**
57+
* List of provider slugs to allow for this request
58+
*/
59+
only?: string[]
60+
/**
61+
* List of provider slugs to skip for this request
62+
*/
63+
ignore?: string[]
64+
/**
65+
* List of quantization levels to filter by (e.g. ["int4", "int8"])
66+
*/
67+
quantizations?: Array<
68+
| 'int4'
69+
| 'int8'
70+
| 'fp4'
71+
| 'fp6'
72+
| 'fp8'
73+
| 'fp16'
74+
| 'bf16'
75+
| 'fp32'
76+
| 'unknown'
77+
>
78+
/**
79+
* Sort providers by price, throughput, or latency
80+
*/
81+
sort?: 'price' | 'throughput' | 'latency'
82+
/**
83+
* Maximum pricing you want to pay for this request
84+
*/
85+
max_price?: {
86+
prompt?: number | string
87+
completion?: number | string
88+
image?: number | string
89+
audio?: number | string
90+
request?: number | string
91+
}
92+
}
4193

94+
export type OpenRouterProviderOptions = {
95+
models?: string[]
96+
reasoning?: OpenRouterReasoningOptions
4297
/**
4398
* A unique identifier representing your end-user, which can
4499
* help OpenRouter to monitor and detect abuse.
@@ -57,7 +112,8 @@ export type AgentTemplate<
57112
id: AgentTemplateType
58113
displayName: string
59114
model: Model
60-
reasoningOptions?: OpenRouterProviderOptions['reasoning']
115+
reasoningOptions?: OpenRouterReasoningOptions
116+
providerOptions?: OpenRouterProviderRoutingOptions
61117

62118
mcpServers: Record<string, MCPConfig>
63119
toolNames: (ToolName | (string & {}))[]

common/src/types/contracts/llm.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ParamsExcluding } from '../function-params'
55
import type { Logger } from './logger'
66
import type { Model } from '../../old-constants'
77
import type { Message } from '../messages/codebuff-message'
8+
import type { OpenRouterProviderRoutingOptions } from '../agent-template'
89
import type { generateText, streamText } from 'ai'
910
import type z from 'zod/v4'
1011

@@ -36,6 +37,7 @@ export type PromptAiSdkStreamFn = (
3637
maxRetries?: number
3738
onCostCalculated?: (credits: number) => Promise<void>
3839
includeCacheControl?: boolean
40+
agentProviderOptions?: OpenRouterProviderRoutingOptions
3941
sendAction: SendActionFn
4042
logger: Logger
4143
trackEvent: TrackEventFn
@@ -57,6 +59,7 @@ export type PromptAiSdkFn = (
5759
agentId?: string
5860
onCostCalculated?: (credits: number) => Promise<void>
5961
includeCacheControl?: boolean
62+
agentProviderOptions?: OpenRouterProviderRoutingOptions
6063
maxRetries?: number
6164
sendAction: SendActionFn
6265
logger: Logger
@@ -82,6 +85,7 @@ export type PromptAiSdkStructuredInput<T> = {
8285
agentId?: string
8386
onCostCalculated?: (credits: number) => Promise<void>
8487
includeCacheControl?: boolean
88+
agentProviderOptions?: OpenRouterProviderRoutingOptions
8589
maxRetries?: number
8690
sendAction: SendActionFn
8791
logger: Logger

common/src/types/dynamic-agent-template.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,41 @@ export const DynamicAgentDefinitionSchema = z.object({
134134
]),
135135
)
136136
.optional(),
137+
providerOptions: z
138+
.object({
139+
order: z.array(z.string()).optional(),
140+
allow_fallbacks: z.boolean().optional(),
141+
require_parameters: z.boolean().optional(),
142+
data_collection: z.enum(['allow', 'deny']).optional(),
143+
only: z.array(z.string()).optional(),
144+
ignore: z.array(z.string()).optional(),
145+
quantizations: z
146+
.array(
147+
z.enum([
148+
'int4',
149+
'int8',
150+
'fp4',
151+
'fp6',
152+
'fp8',
153+
'fp16',
154+
'bf16',
155+
'fp32',
156+
'unknown',
157+
]),
158+
)
159+
.optional(),
160+
sort: z.enum(['price', 'throughput', 'latency']).optional(),
161+
max_price: z
162+
.object({
163+
prompt: z.union([z.number(), z.string()]).optional(),
164+
completion: z.union([z.number(), z.string()]).optional(),
165+
image: z.union([z.number(), z.string()]).optional(),
166+
audio: z.union([z.number(), z.string()]).optional(),
167+
request: z.union([z.number(), z.string()]).optional(),
168+
})
169+
.optional(),
170+
})
171+
.optional(),
137172

138173
// Tools and spawnable agents
139174
mcpServers: z.record(z.string(), mcpConfigSchema).default(() => ({})),

packages/agent-runtime/src/prompt-agent-stream.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ export const getAgentStreamFromTemplate = (params: {
106106
).reasoning = template.reasoningOptions
107107
}
108108

109+
// Pass agent's provider routing options to SDK
110+
aiSdkStreamParams.agentProviderOptions = template.providerOptions
111+
109112
return promptAiSdkStream(aiSdkStreamParams)
110113
}
111114

0 commit comments

Comments
 (0)