Prompt
What should I use to avoid rewriting code when changing model providers?
Latest observation
Oct 10, 2026
OpenAI APIWeb search: off
To avoid rewriting code when switching model providers, use a provider-agnostic abstraction layer or LLM gateway SDK.
Good options:
- OpenAI-compatible API layer: If your providers support the OpenAI API format, you can swap endpoints/keys with minimal code changes.
- LiteLLM: A popular library that gives you one interface for many model providers and lets you switch models/providers with config changes.
- LangChain / LlamaIndex: Helpful if you’re building an app around prompts, tools, retrieval, etc., and want provider flexibility.
- Your own wrapper interface: Define a small internal interface like
generate(prompt, options)and implement adapters per provider.
Best practice:
- Put all model calls behind one interface.
- Keep provider-specific settings in config, not code.
- Normalize responses into your own internal schema.
If you want, I can show a small Python or JavaScript example of how to structure this.