The Wild

Migrate an AI App Between Model Providers

· 6 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Switching an AI application from one model provider (e.g., OpenAI to Anthropic, or Azure to Google Vertex) is mostly about isolating the provider-specific code behind a common interface, then updating authentication, request/response formats, and testing thoroughly. The goal is to minimize changes to your business logic and data flow. Before you start, you'll need access to the new provider's API keys, documentation for both providers' SDKs, and a way to run your existing test suite.

What You'll Need

  • Active API keys for both the old and new providers
  • Access to the codebase where the AI calls are made (or at least the service layer)
  • Documentation for both providers' REST or SDK interfaces (endpoints, request/response schemas, rate limits)
  • A test environment with sample inputs/outputs to verify behavior
  • Optional: a feature flag or environment variable to toggle providers during rollout

Step-by-Step

  1. Map all AI integration points. Search your codebase for every place you call the old provider's SDK or API. Look for imports, client initializations, and direct HTTP calls. List them in a spreadsheet or doc—this becomes your checklist.
  2. Define a common interface. Create an abstraction layer (e.g., a class or function) that your app will use for all AI tasks. It should expose methods like generateText(prompt, options), embed(text), or chat(messages), independent of any provider. This is the single most important step—it prevents vendor lock-in later.
  3. Implement the new provider adapter. Write a class that implements that interface using the new provider's SDK or REST API. Map your existing input parameters (temperature, max tokens, stop sequences) to the new provider's equivalents—they often have different names (e.g., max_tokens vs max_output_tokens).
  4. Handle authentication and configuration. Update your environment variables or config files to store the new provider's API key and endpoint. If you use a secrets manager, rotate the key there. Ensure your code reads the new credentials, not the old ones.
  5. Translate request and response formats. The new provider may expect different JSON structures. For example, some use messages with roles, others use a single prompt string. Write conversion functions to normalize incoming requests and outgoing responses to your internal schema. Pay attention to error objects—they differ in shape.
  6. Update streaming and async handling. If your app streams responses (token-by-token), check how the new provider delivers streams (SSE, WebSocket, etc.). Adapt your client code to consume the new stream format and emit the same events to your UI.
  7. Adjust for rate limits and quotas. The new provider may have different rate limits (requests per minute, tokens per minute). Update your retry logic and backoff strategies. If you use a queue, adjust its throughput.
  8. Run a side-by-side comparison. Feed identical test prompts to both the old and new implementations. Compare outputs for quality, latency, and cost (if you track it). Document any differences—they may affect your product's behavior.
  9. Update your test suite. Replace mock responses from the old provider with those from the new one. Add integration tests that hit the real API (in a sandbox) to catch schema mismatches. Run your full regression suite.
  10. Deploy behind a feature flag. If possible, ship the new provider behind a flag so you can switch back instantly if issues arise. Start with a small percentage of traffic, monitor error rates and latency, then ramp up.
  11. Remove the old provider code. After a stable period (e.g., a week), delete the old adapter and its SDK dependencies. Update documentation and any CI/CD pipelines that referenced the old provider.

Common Mistakes

  • Hardcoding provider-specific logic deep in business code. If you didn't abstract early, you'll have to refactor many files—do it now.
  • Assuming parameter names match. temperature is common, but top_p, frequency_penalty, and presence_penalty may not exist or have different ranges. Always check docs.
  • Forgetting about token counting. Different providers count tokens differently (e.g., character vs subword). Your cost estimation and context window limits may shift.
  • Ignoring response format differences. Some providers return choices[0].message.content, others return output directly. A simple field rename can break your app.
  • Skipping error handling. The new provider may return HTTP 429 (rate limit) or 500 more often. Your existing retry logic might not catch their specific error codes.
  • Not testing with real data. Mock responses often hide subtle differences in formatting, whitespace, or encoding.

Tips

  • Start with a small, non-critical feature. Migrate one endpoint or one function first to learn the new provider's quirks before tackling everything.
  • Use a provider-agnostic SDK or library like LangChain or LiteLLM if you're already using one—they often support multiple providers with minimal changes.
  • Log all requests and responses during the transition to compare behavior in production.
  • Check the new provider's pricing model—some charge per token differently, which could affect your costs significantly.
  • Keep the old provider's API key active for a few weeks after migration in case you need to roll back.
  • Read the new provider's documentation on model versioning—they may deprecate models faster than you expect.

Troubleshooting

  • Authentication errors (401/403): Double-check your API key is correct and has the right permissions. Some providers require separate keys for different models or regions. Also verify the key is being sent in the correct header (e.g., Authorization: Bearer vs x-api-key).
  • Invalid request (400): Compare your request payload to the new provider's schema. Common issues: missing required fields, wrong data types, or unsupported parameters. Use a JSON validator and the provider's API playground to test.
  • Unexpected output format: Log the raw response and inspect it. Write a small parsing function to extract the text, and handle cases where the response is nested differently than expected.
  • Rate limit errors (429): Implement exponential backoff with jitter. If you're hitting limits, consider batching requests or reducing concurrency. Some providers offer quota increases on request.
  • Streaming stops mid-response: Check if the new provider sends a done event or a specific end-of-stream marker. Your client may be waiting for a different signal.
  • Latency spikes: The new provider's first token latency may be higher. Warm up connections (keep-alive) and consider using a regional endpoint closer to your users.
  • Model behavior differs: The new model may be more or less verbose, or handle prompts differently. Adjust your prompts or system messages to match. Use a few-shot evaluation set to quantify the difference.

Frequently Asked Questions

Do I need to rewrite all my prompts?

Not necessarily, but you should test them. Different models are trained differently, so the same prompt may yield different results. You may need to tweak wording or add more context.

Can I run both providers at the same time?

Yes, using a feature flag or a routing layer. This is a common strategy for gradual migration and A/B testing.

How long does a typical migration take?

It depends on the number of integration points and the complexity of your app. A simple app with one API call can be done in a day; a complex system with streaming, embeddings, and multiple models could take a week or more.

What about fine-tuned models?

If you fine-tuned a model on the old provider, you'll need to retrain on the new provider's platform. This requires exporting your training data and re-running the fine-tuning process.

Will my costs change?

Almost certainly. Different providers have different pricing per token, and models have different tokenization rates. Calculate your expected usage and compare pricing pages.

What if the new provider has a feature my app depends on?

Check the new provider's feature list before migrating. If a critical feature (like function calling or JSON mode) is missing, you may need to implement a workaround or choose a different provider.