Prompt Engineering and API Integration · Lesson 3
API Integration with LLMs
Every major provider exposes an HTTP API. You send a request containing the model name, a list of messages, and settings such as max tokens and temperature; you get back the generated message plus usage counts.
Use the official SDK for your language. Keep API keys in environment variables or a secrets manager — never in source code or the browser. Wrap calls with timeouts and retries with exponential backoff, because rate limits (HTTP 429) and transient errors (5xx) are normal.
Track token usage per request. Cost is usually priced separately for input and output tokens, so long prompts and long answers both cost money.
Key points
- Requests = model + messages + parameters
- Store keys in env vars, call the API from a server
- Retry 429/5xx errors with exponential backoff
- Log token usage to control cost
import os, anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
resp = client.messages.create(
model=MODEL_NAME,
max_tokens=500,
system="You are a concise tutor.",
messages=[{"role": "user", "content": "Explain embeddings in 3 lines."}],
)
print(resp.content[0].text)
print(resp.usage.input_tokens, resp.usage.output_tokens)