𝗪𝗵𝘆 𝗜 𝗦𝘁𝗼𝗽𝗽𝗲𝗱 𝗛𝗮𝗿𝗱𝗰𝗼𝗱𝗶𝗻𝗴 𝗔𝗜 𝗔𝗣𝗜 𝗖𝗮𝗹𝗹𝘀

It was 11 PM. I built a tool for developers. OpenAI worked. Then a client asked for Claude.

My code had one function. It called OpenAI directly. To add Claude, I had to copy and paste. This created bad code.

I used if-else blocks first. It worked for two providers. The third provider broke everything. The code became a mess.

I built a simple interface. Now the app asks for a prompt. It does not care which AI answers.

Here is the logic:

  • Create a base class for all providers.
  • Build separate classes for OpenAI and Claude.
  • Use a client to call the interface.
  • Use a config file to switch providers.

This approach has limits:

  • Do not do this for one provider.
  • Unique AI features get hidden.
  • Errors vary by provider.

Start simple. Write one provider. Refactor when you need a second one.

How do you handle multiple AI providers? Do you use tools or build your own?

Source: https://dev.to/__c1b9e06dc90a7e0a676b/why-i-stopped-hardcoding-ai-api-calls-and-built-a-simple-abstraction-layer-27me