OpenAI API vs Anthropic API vs Gemini API: Developer Comparison 2026
Compare LLM APIs for developers: pricing, rate limits, SDKs, and production patterns
OpenAI API vs Anthropic API vs Google Gemini API: Developer Guide 2026
Selecting the right LLM API provider affects your entire application architecture. Here's a developer-focused comparison covering real code examples.
Setup
python
OpenAI
from openai import OpenAI
openai_client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])Anthropic
import anthropic
anthropic_client = anthropic.Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])Google Gemini
import google.generativeai as genai
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
gemini_model = genai.GenerativeModel('gemini-2.5-pro')
Streaming Responses
python
OpenAI streaming
with openai_client.chat.completions.stream(
model='gpt-5',
messages=[{'role': 'user', 'content': 'Explain transformers in ML'}]
) as stream:
for text in stream.text_stream:
print(text, end='', flush=True)Anthropic streaming
with anthropic_client.messages.stream(
model='claude-sonnet-4-5',
max_tokens=2000,
messages=[{'role': 'user', 'content': 'Explain transformers in ML'}]
) as stream:
for text in stream.text_stream:
print(text, end='', flush=True)Gemini streaming
for chunk in gemini_model.generate_content('Explain transformers in ML', stream=True):
print(chunk.text, end='', flush=True)
Function Calling
python
OpenAI function calling
tools = [{'type': 'function', 'function': {
'name': 'get_weather',
'description': 'Get current weather',
'parameters': {'type': 'object', 'properties': {'city': {'type': 'string'}}, 'required': ['city']}
}}]response = openai_client.chat.completions.create(
model='gpt-5',
messages=[{'role': 'user', 'content': 'Weather in Tokyo?'}],
tools=tools, tool_choice='auto'
)
if response.choices[0].message.tool_calls:
tc = response.choices[0].message.tool_calls[0]
print(f'Call: {tc.function.name}({tc.function.arguments})')
Anthropic tool use
tools_claude = [{'name': 'get_weather', 'description': 'Get current weather',
'input_schema': {'type': 'object', 'properties': {'city': {'type': 'string'}}, 'required': ['city']}}]response = anthropic_client.messages.create(
model='claude-sonnet-4-5', max_tokens=1000,
tools=tools_claude,
messages=[{'role': 'user', 'content': 'Weather in Tokyo?'}]
)
for block in response.content:
if block.type == 'tool_use':
print(f'Tool: {block.name}, Input: {block.input}')
Structured Output
python
from pydantic import BaseModel
from typing import Listclass TechStack(BaseModel):
languages: List[str]
frameworks: List[str]
databases: List[str]
OpenAI - native structured output (most reliable)
response = openai_client.beta.chat.completions.parse(
model='gpt-5',
messages=[{'role': 'user', 'content': 'Extract tech stack: Python/Django/PostgreSQL/AWS'}],
response_format=TechStack
)
stack = response.choices[0].message.parsed
print(stack.languages) # ['Python']
Rate Limits & Retry
python
import time
import randomdef with_retry(func, max_retries: int = 5):
for attempt in range(max_retries):
try:
return func()
except (openai.RateLimitError, anthropic.RateLimitError):
if attempt == max_retries - 1:
raise
wait = (2 ** attempt) + random.uniform(0, 1)
time.sleep(wait)
Cost Comparison 2026
Provider Selection Guide
Conclusion
Start with OpenAI for its mature SDK and ecosystem. Add Anthropic for long-document and critical reasoning tasks. Use Gemini Flash for high-volume cost-sensitive operations requiring large context windows.
Also available in 中文.