pydantic_ai.models.openrouter
Setup
For details on how to set up authentication with this model, see model configuration for OpenRouter.
KnownOpenRouterProviders
module-attribute
KnownOpenRouterProviders = Literal[
"z-ai",
"cerebras",
"venice",
"moonshotai",
"morph",
"stealth",
"wandb",
"klusterai",
"openai",
"sambanova",
"amazon-bedrock",
"mistral",
"nextbit",
"atoma",
"ai21",
"minimax",
"baseten",
"anthropic",
"featherless",
"groq",
"lambda",
"azure",
"ncompass",
"deepseek",
"hyperbolic",
"crusoe",
"cohere",
"mancer",
"avian",
"perplexity",
"novita",
"siliconflow",
"switchpoint",
"xai",
"inflection",
"fireworks",
"deepinfra",
"inference-net",
"inception",
"atlas-cloud",
"nvidia",
"alibaba",
"friendli",
"infermatic",
"targon",
"ubicloud",
"aion-labs",
"liquid",
"nineteen",
"cloudflare",
"nebius",
"chutes",
"enfer",
"crofai",
"open-inference",
"phala",
"gmicloud",
"meta",
"relace",
"parasail",
"together",
"google-ai-studio",
"google-vertex",
]
Known providers in the OpenRouter marketplace
OpenRouterProviderName
module-attribute
OpenRouterProviderName = str | KnownOpenRouterProviders
Possible OpenRouter provider names.
Since OpenRouter is constantly updating their list of providers, we explicitly list some known providers but allow any name in the type hints. See the OpenRouter API for a full list.
OpenRouterTransforms
module-attribute
OpenRouterTransforms = Literal['middle-out']
Available messages transforms for OpenRouter models with limited token windows.
Currently only supports 'middle-out', but is expected to grow in the future.
OpenRouterCacheTTL
module-attribute
Cache breakpoint time-to-live for OpenRouter prompt caching.
True selects the default TTL ('5m'); '5m' or '1h' may be given explicitly. The TTL is only
forwarded to downstream providers that support it (Anthropic); it is omitted for Gemini.
OpenRouterProviderConfig
Bases: TypedDict
Represents the 'Provider' object from the OpenRouter API.
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
order
instance-attribute
order: list[OpenRouterProviderName]
List of provider slugs to try in order (e.g. ["anthropic", "openai"]). See details
allow_fallbacks
instance-attribute
allow_fallbacks: bool
Whether to allow backup providers when the primary is unavailable. See details
require_parameters
instance-attribute
require_parameters: bool
Only use providers that support all parameters in your request.
data_collection
instance-attribute
data_collection: Literal['allow', 'deny']
Control whether to use providers that may store data. See details
zdr
instance-attribute
zdr: bool
Restrict routing to only ZDR (Zero Data Retention) endpoints. See details
only
instance-attribute
only: list[OpenRouterProviderName]
List of provider slugs to allow for this request. See details
ignore
instance-attribute
List of provider slugs to skip for this request. See details
quantizations
instance-attribute
quantizations: list[
Literal[
"int4",
"int8",
"fp4",
"fp6",
"fp8",
"fp16",
"bf16",
"fp32",
"unknown",
]
]
List of quantization levels to filter by (e.g. ["int4", "int8"]). See details
sort
instance-attribute
sort: Literal['price', 'throughput', 'latency']
Sort providers by price or throughput. (e.g. "price" or "throughput"). See details
max_price
instance-attribute
max_price: _OpenRouterMaxPrice
The maximum pricing you want to pay for this request. See details
OpenRouterReasoning
Bases: TypedDict
Configuration for reasoning tokens in OpenRouter requests.
Reasoning tokens allow models to show their step-by-step thinking process. You can configure this using either OpenAI-style effort levels or Anthropic-style token limits, but not both simultaneously.
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
effort
instance-attribute
effort: Literal[
"xhigh", "high", "medium", "low", "minimal", "none"
]
OpenAI-style reasoning effort level. Cannot be used with max_tokens.
max_tokens
instance-attribute
max_tokens: int
Anthropic-style specific token limit for reasoning. Cannot be used with effort.
exclude
instance-attribute
exclude: bool
Whether to exclude reasoning tokens from the response. Default is False. All models support this.
enabled
instance-attribute
enabled: bool
Whether to enable reasoning with default parameters. Default is inferred from effort or max_tokens.
OpenRouterUsageConfig
Bases: TypedDict
Configuration for OpenRouter usage.
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
247 248 249 250 | |
OpenRouterModelSettings
Bases: ModelSettings
Settings used for an OpenRouter model request.
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
openrouter_models
instance-attribute
A list of fallback models.
These models will be tried, in order, if the main model returns an error. See details
openrouter_provider
instance-attribute
openrouter_provider: OpenRouterProviderConfig
OpenRouter routes requests to the best available providers for your model. By default, requests are load balanced across the top providers to maximize uptime.
You can customize how your requests are routed using the provider object. See more
openrouter_preset
instance-attribute
openrouter_preset: str
Presets allow you to separate your LLM configuration from your code.
Create and manage presets through the OpenRouter web application to control provider routing, model selection, system prompts, and other parameters, then reference them in OpenRouter API requests. See more
openrouter_transforms
instance-attribute
openrouter_transforms: list[OpenRouterTransforms]
To help with prompts that exceed the maximum context size of a model.
Transforms work by removing or truncating messages from the middle of the prompt, until the prompt fits within the model's context window. See more
openrouter_reasoning
instance-attribute
openrouter_reasoning: OpenRouterReasoning
To control the reasoning tokens in the request.
The reasoning config object consolidates settings for controlling reasoning strength across different models. See more
openrouter_usage
instance-attribute
openrouter_usage: OpenRouterUsageConfig
To control the usage of the model.
The usage config object consolidates settings for enabling detailed usage information. See more
openrouter_cache_instructions
instance-attribute
openrouter_cache_instructions: OpenRouterCacheTTL
Whether to add cache_control to stable system instructions.
When enabled, supported downstream providers (Anthropic, Gemini) can cache stable
system instructions and reduce costs. If dynamic instructions are present, the cache
point is placed before them, matching Anthropic's static-prefix caching behavior.
For Gemini models, this setting is ignored when dynamic instructions are present because
OpenRouter normalizes system/developer messages into a single immutable systemInstruction.
Ignored for other downstream providers.
If True, uses TTL='5m'. You can also specify '5m' or '1h' directly.
TTL is only included for Anthropic models; Gemini does not support explicit TTL.
See https://openrouter.ai/docs/guides/best-practices/prompt-caching for more information.
openrouter_cache_messages
instance-attribute
openrouter_cache_messages: OpenRouterCacheTTL
Convenience setting to enable caching for the last message in the conversation.
When enabled, this automatically adds cache_control to the last content block
in the final message (regardless of role), which is useful for Anthropic's prefix-based
caching in multi-turn conversations. In tool-use flows, this may target a tool result
message rather than a user message, which is correct for prefix caching.
Ignored for downstream providers that do not support explicit cache control.
If True, uses TTL='5m'. You can also specify '5m' or '1h' directly.
TTL is only included for Anthropic models; Gemini does not support explicit TTL.
Note: OpenRouter uses only the last breakpoint across normal message content for
Gemini caching. Use this when caching the final message boundary is intentional;
use openrouter_cache_instructions for stable system context. Anthropic supports
prefix-based caching across multi-turn conversations with this setting.
See https://openrouter.ai/docs/guides/best-practices/prompt-caching for more information.
openrouter_cache_tool_definitions
instance-attribute
openrouter_cache_tool_definitions: OpenRouterCacheTTL
Whether to add cache_control to the last tool definition.
When enabled, the last tool in the tools array will have cache_control set,
allowing supported downstream providers to cache tool definitions and reduce costs.
Ignored for downstream providers that do not support explicit tool definition caching.
If True, uses TTL='5m'. You can also specify '5m' or '1h' directly.
TTL is only included for Anthropic models.
Currently only effective for Anthropic models via OpenRouter, as tool definition caching is not documented for other providers.
See https://openrouter.ai/docs/guides/best-practices/prompt-caching for more information.
OpenRouterModel
Bases: OpenAIChatModel
Extends OpenAIChatModel to capture extra metadata for Openrouter.
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 | |
__init__
__init__(
model_name: str,
*,
provider: (
Literal["openrouter"] | Provider[AsyncOpenAI]
) = "openrouter",
profile: ModelProfileSpec | None = None,
settings: ModelSettings | None = None
)
Initialize an OpenRouter model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
The name of the model to use. |
required |
provider
|
Literal['openrouter'] | Provider[AsyncOpenAI]
|
The provider to use for authentication and API access. If not provided, a new provider will be created with the default settings. |
'openrouter'
|
profile
|
ModelProfileSpec | None
|
The model profile to use. Defaults to a profile picked by the provider based on the model name. |
None
|
settings
|
ModelSettings | None
|
Model-specific settings that will be used as defaults for this model. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 | |
supported_native_tools
classmethod
supported_native_tools() -> (
frozenset[type[AbstractNativeTool]]
)
Return the set of builtin tool types this model can handle.
OpenRouter supports web search via its plugins system.
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
868 869 870 871 872 873 874 875 | |
OpenRouterStreamedResponse
dataclass
Bases: OpenAIStreamedResponse
Implementation of StreamedResponse for OpenRouter models.
Source code in pydantic_ai_slim/pydantic_ai/models/openrouter.py
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 | |