From 4535b5847d3cd950b393e4ffb78ae6886feaa2f9 Mon Sep 17 00:00:00 2001 From: Shanto Mathew Date: Mon, 27 Oct 2025 15:35:52 -0500 Subject: [PATCH] docs(openrouter): add base_url config with environment variables (#15946) Added a new "Configuration with Environment Variables" section demonstrating: - Using os.getenv() to dynamically retrieve OpenRouter configuration - Explicitly passing base_url parameter with environment variables - Benefits of this approach for managing configs across environments This helps users implement production-ready configuration patterns. --- docs/my-website/docs/providers/openrouter.md | 33 +++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/my-website/docs/providers/openrouter.md b/docs/my-website/docs/providers/openrouter.md index 58a87f68495..327634909b3 100644 --- a/docs/my-website/docs/providers/openrouter.md +++ b/docs/my-website/docs/providers/openrouter.md @@ -9,10 +9,9 @@ LiteLLM supports all the text / chat / vision models from [OpenRouter](https://o ```python import os from litellm import completion + os.environ["OPENROUTER_API_KEY"] = "" os.environ["OPENROUTER_API_BASE"] = "" # [OPTIONAL] defaults to https://openrouter.ai/api/v1 - - os.environ["OR_SITE_URL"] = "" # [OPTIONAL] os.environ["OR_APP_NAME"] = "" # [OPTIONAL] @@ -22,8 +21,32 @@ response = completion( ) ``` -## OpenRouter Completion Models +## Configuration with Environment Variables +For production environments, you can dynamically configure the base_url using environment variables: + +```python +import os +from litellm import completion + +# Configure with environment variables +OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY") +OPENROUTER_BASE_URL = os.getenv("OPENROUTER_API_BASE", "https://openrouter.ai/api/v1") + +# Set environment for LiteLLM +os.environ["OPENROUTER_API_KEY"] = OPENROUTER_API_KEY +os.environ["OPENROUTER_API_BASE"] = OPENROUTER_BASE_URL + +response = completion( + model="openrouter/google/palm-2-chat-bison", + messages=messages, + base_url=OPENROUTER_BASE_URL # Explicitly pass base_url for clarity +) +``` + +This approach provides better flexibility for managing configurations across different environments (dev, staging, production) and makes it easier to switch between self-hosted and cloud endpoints. + +## OpenRouter Completion Models 🚨 LiteLLM supports ALL OpenRouter models, send `model=openrouter/` to send it to open router. See all openrouter models [here](https://openrouter.ai/models) | Model Name | Function Call | @@ -40,12 +63,12 @@ response = completion( | openrouter/meta-llama/llama-2-70b-chat | `completion('openrouter/meta-llama/llama-2-70b-chat', messages)` | `os.environ['OR_SITE_URL']`,`os.environ['OR_APP_NAME']`,`os.environ['OPENROUTER_API_KEY']` | ## Passing OpenRouter Params - transforms, models, route - Pass `transforms`, `models`, `route`as arguments to `litellm.completion()` ```python import os from litellm import completion + os.environ["OPENROUTER_API_KEY"] = "" response = completion( @@ -54,4 +77,4 @@ response = completion( transforms = [""], route= "" ) -``` \ No newline at end of file +```