Messages APIs and Provider SDKs
Make your first real calls to Claude, OpenAI and Gemini APIs and understand the shared shape: system prompt, message roles and response metadata.
Beginner · about 5 hours · difficulty 2/5
Learn first: HTTP, REST APIs and JSON, Tokens and Context Windows
All major providers expose a similar chat-style interface: you send a list of messages with roles (system or developer instructions, user, assistant) and get back an assistant message plus metadata.
The shared anatomy of a request
- Model name: read it from config; model versions change often.
- System prompt: persistent instructions and persona. Some APIs pass it as a separate field, others as a message with a special role.
- Messages: alternating user and assistant turns. The API is stateless; you resend the history each time (some providers offer server-side conversation state as an option).
- Parameters: max tokens, temperature, stop sequences.
The response
- The generated content (often a list of content blocks: text, tool calls, images).
- A stop reason such as end of turn, max tokens reached, or tool use requested.
- Usage: input and output token counts. Log these on every call; they are your cost data.
SDKs
Use the official SDKs (Anthropic, OpenAI, Google Gen AI) rather than raw HTTP; they handle retries, typing and streaming. Many providers and local servers also offer an OpenAI-compatible endpoint, which is handy but may not expose provider-specific features. Keep keys in environment variables and call models from your server, never directly from a browser.
Good enough to move on
You can make a multi-turn conversation work with at least two providers, log token usage, and handle the max-tokens stop reason.
Key concepts
- Anthropic Claude API
- OpenAI API
- Google Gemini API
- System prompts and message roles
- Stateless multi-turn conversations
- Stop reasons and usage metadata
- OpenAI-compatible endpoints
- Cloud platforms (Bedrock, Vertex, Azure)
Mini project
Build a terminal chatbot that keeps multi-turn history, lets you switch between two providers with a flag, and prints token usage and estimated cost after every turn.
Check yourself
If the API is stateless, how does a chatbot remember what the user said three turns ago, and what problem does this create for long chats?
Free resources
- Anthropic API documentation (Anthropic)
- OpenAI platform documentation (OpenAI)
- Gemini API documentation (Google)