Introduction
Developing with AI models is no longer exotic—today, the APIs of major providers have become a standard tool in every developer's arsenal. OpenAI, Anthropic, and DeepSeek offer powerful capabilities for text generation, data analysis, and automation. But how do you choose the right solution without getting lost in the settings? In this guide, we'll break down the key aspects of integration: authentication, streaming, system prompts, and pricing. You'll learn how to quickly connect each API and avoid common pitfalls.
OpenAI API: Power and Flexibility
The OpenAI API is one of the most popular tools for working with generative AI. It supports models like GPT-4, GPT-4o, and lighter versions for low-latency tasks.
Authentication and Keys
To work with the OpenAI API, you need an API key, which is created in your personal account on the platform. Use the Authorization: Bearer YOUR_API_KEY header in each request. It is recommended to store keys in environment variables rather than in code.
Streaming and System Prompts
Streaming (streaming responses) is implemented via the stream: true parameter. This is especially useful for chatbots and interfaces where output speed matters. System prompts are set via the system field in the messages array—they define the model's behavior, such as tone or role setting.
Example request:
import openai
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an experienced copywriter."},
{"role": "user", "content": "Write an article about AI."}
],
stream=True
)
for chunk in response:
print(chunk['choices'][0]['delta'].get('content', ''), end='')
Pricing
OpenAI uses a pay-per-token model (input and output). For example, GPT-4o costs $5 per 1M input tokens and $15 per 1M output tokens. For budget projects, models like GPT-3.5-turbo are suitable, which are 10 times cheaper.
Anthropic API: Safety and Control
Anthropic, the creators of Claude, focus on safety and interpretability. Their API is ideal for tasks where ethics and accuracy are important.
Authentication and Keys
The Anthropic API also uses access keys (x-api-key). Requests are sent to the endpoint https://api.anthropic.com/v1/messages. Note: the API expects a mandatory anthropic-version header (e.g., "2023-06-01").
Streaming and System Prompts
Streaming is enabled with the stream: true parameter. System prompts are set in a separate system field at the request level, not in the messages array. This simplifies model behavior configuration without mixing with the user dialogue.
Example request:
import anthropic
client = anthropic.Anthropic(api_key="YOUR_API_KEY")
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1000,
system="You are an assistant who answers briefly and to the point.",
messages=[
{"role": "user", "content": "Tell me about AI safety."}
],
stream=True
)
for delta in message:
print(delta.delta.text, end='')
Pricing
Anthropic charges per token, but with nuances: input tokens are cheaper than output tokens. For example, Claude 3.5 Sonnet costs $3 per 1M input tokens and $15 per 1M output tokens. DeepSeek API often offers more aggressive pricing, making it attractive for startups.
DeepSeek API: Accessibility and Innovation
DeepSeek is a young but rapidly growing provider that focuses on openness and low cost. Its models (e.g., DeepSeek-V2) show competitive results in benchmarks.
Authentication and Keys
The DeepSeek API uses a standard Bearer token. Endpoint: https://api.deepseek.com/v1/chat/completions. Important: DeepSeek requires the model parameter to be passed in the request body.
Streaming and System Prompts
Streaming is implemented similarly to OpenAI: the stream: true parameter. System prompts are set as a separate message with the system role. DeepSeek supports
Comments