Introduction
Integrating language models into applications is no longer a privilege of giants—today, any developer can connect ChatGPT, Claude, or DeepSeek. But with the growing number of providers, the question arises: which API to choose and how to properly configure the interaction? In this guide, we will break down three leading services—OpenAI, Anthropic, and DeepSeek—and show how authentication, streaming, system prompts, and pricing work. You will learn how to avoid common mistakes and optimize costs.
1. Authentication and Basic Connection
OpenAI API
- Key: obtained at OpenAI Platform.
- Example request (Python):
python from openai import OpenAI client = OpenAI(api_key='your-key') response = client.chat.completions.create( model='gpt-4', messages=[{'role': 'user', 'content': 'Hello'}] ) - Feature: uses
chat.completions, supportsstream=True.
Anthropic API
- Key: created at Anthropic Console.
- Example request:
python import anthropic client = anthropic.Anthropic(api_key='your-key') message = client.messages.create( model='claude-3-5-sonnet-20241022', max_tokens=1024, messages=[{'role': 'user', 'content': 'Hello'}] ) - Important: the
max_tokensparameter is mandatory.
DeepSeek API
- Key: registration at platform.deepseek.com.
- Example request:
python import requests response = requests.post( 'https://api.deepseek.com/v1/chat/completions', headers={'Authorization': 'Bearer your-key'}, json={'model': 'deepseek-chat', 'messages': [{'role': 'user', 'content': 'Hello'}]} ) - Compatibility: the API is partially compatible with OpenAI, simplifying migration.
2. Streaming: How to Get Responses in Real Time
Streaming is critical for chat interfaces and assistants. Let's look at the implementation for each provider.
OpenAI
stream = client.chat.completions.create(
model='gpt-4',
messages=[{'role': 'user', 'content': 'Tell me about AI'}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end='')
Anthropic
with client.messages.stream(
model='claude-3-5-sonnet-20241022',
max_tokens=1024,
messages=[{'role': 'user', 'content': 'Tell me about AI'}]
) as stream:
for text in stream.text_stream:
print(text, end='')
DeepSeek
response = requests.post(
'https://api.deepseek.com/v1/chat/completions',
headers={'Authorization': 'Bearer your-key'},
json={'model': 'deepseek-chat', 'messages': [{'role': 'user', 'content': 'Tell me about AI'}], 'stream': True},
stream=True
)
for line in response.iter_lines():
if line:
print(line.decode(), end='')
3. System Prompts: Configuring Model Behavior
System prompts set the tone and rules for the AI. They are implemented differently in each API.
- OpenAI: passed as
{'role': 'system', 'content': 'You are a Python expert'}. - Anthropic: uses
systemas a separate parameter:system='You are a Python expert'. - DeepSeek: similar to OpenAI, supports
systemin themessagesarray.
Tip: for complex tasks, combine system prompts with few-shot examples—this will improve response accuracy.
4. Pricing and Cost Optimization
The cost of APIs varies depending on the model and token volume.
| Provider | Model | Price per 1M input tokens | Price per 1M output tokens |
|---|---|---|---|
| OpenAI | GPT-4o | $2.50 | $10.00 |
| Anthropic | Claude 3.5 Sonnet | $3.00 | $15.00 |
| DeepSeek | DeepSeek V2 | $0.14 | $0.28 |
Savings recommendations:
- Use caching for frequent requests.
- Choose models with smaller context windows for simple tasks.
- For DeepSeek, it is typical
Comments