Accessing Multiple AI Models With the OpenRouter API: Getting Started & Using Intelligent Routing
Skills:
LLM Foundations70%
Key Takeaways
Accessing multiple AI models with the OpenRouter API
Full Transcript
Hello. Welcome to accessing multiple AI models with the OpenRouter API. My name's Joseph and I'll be your instructor for this video course. That's right. I still haven't been replaced by AI. Yet. But I've been using it and I bet you have, too. So, you know that anyone looking to integrate AI into their projects or workflows first has to answer one question. Which model should I use? And anyone who's tried to answer that question has been faced with about a thousand more. Which AI provider to use? How are models differentiated? What about costs? What APIs do I need to learn? And if I choose one model and I have issues, how hard will it be to switch to another? What's more, at the speed that AI is currently moving, by the time you work your way through these questions, the answers have probably changed again. This is where OpenRouter comes in. Instead of writing bespoke code for each AI service, its API provides a routing layer between your application and AI providers. With OpenRouter, you can develop, experiment, and iterate quickly against a single API while exploring hundreds of AI models. In this course, you'll learn how to use Python to work with the OpenRouter API, how to route requests across multiple AI providers, how to choose models automatically or explicitly, and how to implement model fallbacks for reliability. For this course, you should have a good grasp on Python fundamentals. Things like working with modules, built-in data structures, exception handling, and using environment variables in your code. If you'd like a refresher on modules, I recommend the course Python Basics, Modules and Packages. Otherwise, follow me to the next lesson where you'll get set up with OpenRouter. Why use OpenRouter? Well, the AI ecosystem is fragmented. Different AI providers expose different APIs. Across those providers, the authentication schemes are going to vary, as will rules around rate limits and token use. Not to mention, model availability is changing constantly. All of this together makes developing for AI complex and time-consuming. What OpenRouter does is provide you with a unified API across providers. Using OpenRouter, you can access hundreds of AI models, switch between different model providers without changing your code, compare the outputs of multiple models, all of which helps you build more resilient AI applications. So, to get started, you'll need an API key. So, head on over to openrouter.ai and create an account. Once you've created an account, you can get an API key. In the future, this probably looks different, but it's most likely still pretty straightforward. You can follow the big bold get API key button, or you can navigate to workspaces and get your keys from there. Workspaces and view your API keys. From here, hit create. Give your API key a name. Don't worry about credit limit or reset limit for now, since the free plan will be more than enough for this course. I'll be using an expiration for obvious reasons, but you don't need to worry about that either. Click create. And now that you've got your API key, you'll need to save it somewhere secure, because otherwise you won't be able to get it again. Now that you've got your API key, open up your IDE to get started. I'm here in a folder called openrouter-project, and since you'll need to install the requests package, you'll want to have a virtual environment active here. You can create one with python-m venv venv and activate it with source venv/bin/activate. And then install requests, python-m pip install requests. And importantly, export your API key to the variable OPENROUTER_API_KEY. Once you've done that, take a look at get_models.py, which you can find in the course resources along with all of the other scripts we'll be using throughout this course. This short script will validate that your API key is working. It imports OS and requests, defines the OpenRouter models URL endpoint, and collects the OpenRouter API key you just exported. That API key is included in the headers dictionary. That's how you authenticate with the OpenRouter API. Then, you make a get request to the API endpoint, passing in the headers, and save the response as a dictionary by calling the JSON method. Get data from the data dict, returning an empty dictionary if there is no data key. That's error handling. And finally, the script prints out the results of your query with some nicely formatted f-strings. Now, this is important. Run the script from the same shell you used to export the API key. python get_models.py Success. Found 370 models via OpenRouter, followed by a list of some of the available models. Congratulations. You just made your first query with OpenRouter. Most likely, the output you'll see here will differ as time moves on. But what's important is that you see the success message. If you do run into any trouble, first confirm your API key is correct and valid, then verify that the exported variable name is openrouter_api_key exactly, or else it won't work. And if you're still stumped, review the OpenRouter documentation at openrouter.ai/docs. And in the next lesson, you'll query an actual model. See you there. OpenRouter uses intelligent routing to automatically select a model for your prompt. Enable intelligent routing with the following. So, the payload dictionary that you'll use for your request, set the model key to the string openrouter/auto. Then, OpenRouter will analyze your prompt, select an appropriate model, and route the request to the best available provider. If you don't have strong preferences for models or providers, this is a great approach. To see it in action, head back to your IDE. Grab the ask auto model file from the course resources. Here you have a similar script to the one you used before with some important changes. Line four has changed. Now you're targeting the chat completions endpoint, where you'll send a prompt and get a model response. Because you're sending data, you also add content type application/json to the headers dictionary. And the data you're sending is in the payload dictionary. Model is defined as openrouter/auto, meaning open router will choose the model for your request. And the messages key holds a list of dictionaries. This is fairly standard for LLM chats. In this one, the role is user, and the content is briefly explain Star Trek. And instead of calling requests.get, you call requests.post, targeting the open router API URL, passing in headers as headers, and the payload dict as the JSON argument. You store the JSON response as a dict in data, and report the model that was used, as well as the response content, as nicely formatted F-strings. Open up the shell, and before you run the script, unless you're using the same shell as before, make sure to export your API key again. And give it a go. python ask_auto_model.py Not exactly what I'd call brief. Scroll up. And the model used is mistralai/mistral-7b-instruct-v0.1. Not necessarily the same model you'll see if you're following along at home. Remember, because we set model to openrouter/auto, open router automatically selects one of the available models based on the prompt it received. And the response is a very lengthy and thorough explanation of what Star Trek is. Very cool. Of course, sometimes you do have preferences for which models to use. Learn how to apply those preferences in the next lesson.
Original Description
Download your free Python Cheat Sheet here: https://realpython.com/cheatsheet
Free Python Skill Test with instant level + learning plan: https://realpython.com/skill-test
Want to learn faster? Become a Python Expert with unlimited access to 5,000+ tutorials, videos, and exercises: https://realpython.com/start
This is a preview of the video course, "Accessing Multiple AI Models With the OpenRouter API". One of the quickest ways to call multiple AI models from a single Python script is to use OpenRouter’s API, which acts as a unified routing layer between your code and multiple AI providers. By the end of this course, you’ll be able to access models from several providers through one unified API.
This convenience matters because the AI ecosystem is highly fragmented: each provider exposes its own API, authentication scheme, rate limits, and model lineup. Working with multiple providers often requires additional setup and integration effort, especially when you want to experiment with different models, compare outputs, or evaluate trade-offs for a specific task.
OpenRouter gives you access to thousands of models from leading providers like OpenAI, Anthropic, Mistral, Google, and Meta. You can switch between them without changing your application code.
This is a portion of the complete course, which you can find here: https://realpython.com/courses/multiple-ai-models-openrouter-api/
The rest of the course covers:
- Modifying Routing Behavior
- Exercise: Build an OpenRouter Request Payload
- Implementing Model Fallbacks
- Weighing Your Options for Model Selection
- Exercise: Build a Fallback Models Payload
- Accessing Multiple AI Models With the OpenRouter API (Quiz)
🐍 Become a Python expert with real-world tutorials, on-demand courses, interactive quizzes, and 24/7 access to a community of experts at https://realpython.com
▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰
🐍 Start Here → https://realpython.com/start
🗺️ Guided Learning Paths → https://realpython.com/learning-paths
🎧 Rea
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
More on: LLM Foundations
View skill →Related Reads
📰
📰
📰
📰
The Algorithm is Judging You: Why Netflix Keeps Showing You ‘The Office’ and How to Break the…
Medium · AI
The Future of AI: Trends Every Developer Should Watch
Dev.to AI
Why the synthetic-data escape hatch failed and how publishers became toll collectors
Dev.to AI
CLM Series/Chapter-22: Which Professions Are Changing? Which Are Disappearing?
Medium · AI
🎓
Tutor Explanation
DeepCamp AI