Getting Started with ChatGPT and Claude APIs
Large Language Model (LLM) APIs from OpenAI and Anthropic allow you to integrate advanced AI capabilities into any application. Whether you are building a chatbot, automating content creation, analyzing documents, or building a coding assistant, these APIs provide the intelligence layer that powers modern AI applications.
OpenAI ChatGPT API
Setup
- Create an account at
platform.openai.com. - Generate an API key in the API Keys section of your dashboard.
- Install the official SDK for your language:
- Python:
pip install openai - Node.js:
npm install openai - PHP:
composer require openai-php/client
- Python:
- Set your API key as an environment variable:
OPENAI_API_KEY=your_key_here
Basic API Call (Python)
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful e-commerce assistant."},
{"role": "user", "content": "Write a product description for a wireless Bluetooth speaker."}
],
max_tokens=500,
temperature=0.7
)
print(response.choices[0].message.content)
Function Calling
Function calling allows the AI to invoke your application's functions to retrieve real-time data or perform actions. Define functions with JSON schemas, and the model will call them when appropriate during a conversation. This is essential for building AI agents that can look up orders, check inventory, or update records.
Anthropic Claude API
Setup
- Create an account at
console.anthropic.com. - Generate an API key.
- Install the SDK:
- Python:
pip install anthropic - Node.js:
npm install @anthropic-ai/sdk
- Python:
- Set your API key:
ANTHROPIC_API_KEY=your_key_here
Basic API Call (Python)
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=500,
messages=[
{"role": "user", "content": "Write a product description for a wireless Bluetooth speaker."}
],
system="You are a helpful e-commerce assistant."
)
print(message.content[0].text)
Tool Use
Claude's tool use feature works similarly to OpenAI's function calling. Define tools with input schemas, and Claude will generate tool calls when it needs external data. Your application executes the tool and sends the result back to Claude for the final response.
Key API Concepts
Models
| Provider | Model | Best For | Approximate Cost |
|---|---|---|---|
| OpenAI | GPT-4o | Complex reasoning, code | $2.50/$10 per M tokens |
| OpenAI | GPT-4o-mini | Simpler tasks, high volume | $0.15/$0.60 per M tokens |
| Anthropic | Claude Sonnet | Balanced quality and speed | $3/$15 per M tokens |
| Anthropic | Claude Haiku | Fast, cost-effective | $0.25/$1.25 per M tokens |
Temperature
Temperature controls response randomness. Lower values (0.0-0.3) produce consistent, deterministic outputs ideal for data extraction and classification. Higher values (0.7-1.0) produce more creative, varied responses suitable for content generation. For most application use cases, a temperature of 0.3-0.5 works well.
Token Management
Tokens are the unit of measurement for API usage and billing. Roughly, 1 token equals 4 characters in English. Track token usage to manage costs and stay within rate limits. Use the max_tokens parameter to cap response length and prevent runaway costs.
Common Integration Patterns
Content Generation Pipeline
Automate product descriptions, marketing copy, email templates, and blog content. Use system prompts to maintain brand voice consistency across all generated content. Process items in batches and cache results to minimize API calls.
Retrieval-Augmented Generation (RAG)
Combine your own data with AI intelligence. Store your product catalog, documentation, or knowledge base as embeddings in a vector database. When a user asks a question, retrieve relevant documents and include them in the API call context. This grounds the AI's responses in your actual data, dramatically reducing hallucination.
Structured Data Extraction
Extract structured data from unstructured text. Parse customer emails to extract order numbers, product names, and issue types. Analyze reviews to extract sentiment and key themes. Convert natural language queries into database queries.
Best Practices
- Never expose API keys in client-side code. All API calls should go through your backend server.
- Implement request rate limiting to prevent accidental cost spikes.
- Cache responses for identical or similar queries to reduce API costs.
- Use the cheapest model that meets your quality requirements. Start with mini/haiku and upgrade only if needed.
- Log all API requests and responses for debugging and cost monitoring.
- Implement proper error handling for rate limits (429), timeouts, and API errors.
- Set spending limits in your API dashboard to prevent unexpected charges.