LLM API for building AI Agents
I set up a development environment to learn how to use provider APIs to call their LLM models. I use the OpenAI API to call the GPT-5 model and the Anthropic API to call the Claude Sonnet model.
I use the Python uv package manager to install the OpenAI and Anthropic API libraries. I also use the python-dotenv package to load provider token information from environment variables.
Configure and add the following provider's LLM access token:
OpenAI
OpenAI’s Chat Completions API has become an industry standard, and most LLM providers offer similar interfaces. Let’s initialize the OpenAI client and send a simple request.
Anthropic
Anthropic works in a similar way, but there are some differences in how the API is called. OpenAI uses client.chat.completions.create, whereas Anthropic uses client.messages.create. Likewise, the response formats differ: OpenAI returns content in choices[0].message.content, while Anthropic returns it in content[0].text.
LiteLLM
Switching between different LLM providers when building AI agents can be tedious. Each provider offers different interfaces, which can impact both cost and performance requirements. LiteLLM solves this problem by providing a unified approach. It is an open-source library that offers a single interface to call over 100 LLMs.
LiteLLM’s main features include:
- Calling any provider using the same
completion()interface - A consistent output format, regardless of the provider or model used
- Built-in retry and fallback logic across multiple deployments via the Router
- Compatible exception handling across all providers
- Support for observability