{"slug": "how-to-switch-ai-models-without-rewriting-your-app", "title": "How to switch AI models without rewriting your app", "summary": "A developer from TokenBay demonstrates how to switch AI models without rewriting application code by using an OpenAI-compatible API gateway. The approach allows developers to keep the familiar OpenAI SDK style while routing requests to different model families through a single endpoint. Changing the base URL and API key enables testing of models like Claude, Gemini, and DeepSeek with minimal code changes.", "body_md": "Most AI apps start with one model provider.\n\nThat is usually the right choice. For a first version, you want one SDK, one API key, one billing page, and one model name. Simple is good when you are trying to ship.\n\nBut once the product grows, the model decision gets more complicated.\n\nYou may want to test another model because:\n\nThe annoying part is that switching models is often not just changing a string.\n\nIt can mean adding another SDK, another API key, another request format, another dashboard, and another set of provider-specific edge cases.\n\nThat gets messy quickly.\n\nA first version might look like this:\n\n``` python\npython\nfrom openai import OpenAI\n\nclient = OpenAI(\n    api_key=\"YOUR_OPENAI_API_KEY\",\n)\n\nresponse = client.chat.completions.create(\n    model=\"gpt-4o-mini\",\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": \"Write a short onboarding message for a developer tool.\"\n        }\n    ],\n)\n\nprint(response.choices[0].message.content)\n```\n\nThis is clean and totally fine.\n\nBut if you later want to compare Claude, Gemini, DeepSeek, or another model family, you may not want to rewrite your AI integration around each provider.\n\nOne practical option is to use an OpenAI-compatible API gateway.\n\nYour app keeps using the OpenAI SDK style, but the gateway lets you route requests to different model families through one endpoint.\n\nI work on the TokenBay team, so the example below uses TokenBay. The general idea applies to any OpenAI-compatible gateway.\n\n``` python\npython\nfrom openai import OpenAI\n\nclient = OpenAI(\n    base_url=\"https://api.tokenbay.com/v1\",\n    api_key=\"YOUR_TOKENBAY_API_KEY\",\n)\n\nresponse = client.chat.completions.create(\n    model=\"gpt-5.4-mini\",\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": \"Write a short onboarding message for a developer tool.\"\n        }\n    ],\n)\n\nprint(response.choices[0].message.content)\n```\n\nThe main change is just:\n\n```\npython\nbase_url=\"https://api.tokenbay.com/v1\"\napi_key=\"YOUR_TOKENBAY_API_KEY\"\n```\n\nThat is the useful part.\n\nYou keep the familiar OpenAI client shape, but you are no longer wiring every provider separately.\n\nOnce your app uses an OpenAI-compatible endpoint, testing another supported model can be as simple as changing the model name.\n\n```\npython\nresponse = client.chat.completions.create(\n    model=\"claude-sonnet-4.6\",\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": \"Write a short onboarding message for a developer tool.\"\n        }\n    ],\n)\n```\n\nOr:\n\n```\npython\nresponse = client.chat.completions.create(\n    model=\"gemini-2.5-pro\",\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": \"Write a short onboarding message for a developer tool.\"\n        }\n    ],\n)\n```\n\nThe point is not that every model behaves the same.\n\nThey do not.\n\nThe point is that your business logic should not need to change every time you want to compare models.\n\nFor a real app, I would keep the base URL, API key, and model name in environment variables:\n\n```\nbash\nLLM_BASE_URL=https://api.tokenbay.com/v1\nLLM_API_KEY=YOUR_TOKENBAY_API_KEY\nLLM_MODEL=gpt-5.4-mini\n```\n\nThen your application code can stay stable while you test different models.\n\nChange config, redeploy, compare results.\n\n**Very boring. Very useful.**\n\nThis setup is useful if you are:\n\nIt does not magically solve model selection. You still need to test output quality, latency, pricing, context length, and reliability.\n\nBut it does make the integration layer much simpler.\n\nA gateway is not always the right choice.\n\nDirect provider integration may be better if:\n\nThat is a fair tradeoff.\n\nThe point is not **\"always use a gateway.\"**\n\nThe point is this:\n\n**If you are going to test multiple models anyway, your app should not need a rewrite every time.**\n\nTokenBay is an OpenAI-compatible API gateway for accessing models such as GPT, Claude, Gemini, DeepSeek, and others through one endpoint and one API key.\n\nIt includes:\n\nIf you want to test this pattern, you can try TokenBay here:\n\n[Try TokenBay][https://www.tokenbay.com/?utm_source=devto&utm_medium=community_content&utm_campaign=week1_free_content](https://www.tokenbay.com/?utm_source=devto&utm_medium=community_content&utm_campaign=week1_free_content)\n\nCurrent launch offer:\n\nI would love feedback from builders:", "url": "https://wpnews.pro/news/how-to-switch-ai-models-without-rewriting-your-app", "canonical_source": "https://dev.to/gwenj/how-to-switch-ai-models-without-rewriting-your-app-1dc6", "published_at": "2026-06-29 08:47:08+00:00", "updated_at": "2026-06-29 08:57:15.357481+00:00", "lang": "en", "topics": ["large-language-models", "developer-tools", "ai-infrastructure"], "entities": ["TokenBay", "OpenAI", "Claude", "Gemini", "DeepSeek", "GPT"], "alternates": {"html": "https://wpnews.pro/news/how-to-switch-ai-models-without-rewriting-your-app", "markdown": "https://wpnews.pro/news/how-to-switch-ai-models-without-rewriting-your-app.md", "text": "https://wpnews.pro/news/how-to-switch-ai-models-without-rewriting-your-app.txt", "jsonld": "https://wpnews.pro/news/how-to-switch-ai-models-without-rewriting-your-app.jsonld"}}