Choosing Your API
The main choices for production applications: OpenAI (best ecosystem, easy start), Anthropic (best for complex instructions and safety), Google Gemini (best for multimodal and long context), and open-source models via Ollama/Together.ai (best for cost and privacy).
Text Generation with Streaming
import openai
client = openai.OpenAI()
# Streaming — display tokens as they arrive
with client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a haiku about AI agents."}],
stream=True
) as stream:
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
Image Generation
# DALL-E 3
response = client.images.generate(
model="dall-e-3",
prompt="A photorealistic image of a robot reading a book in a cosy library",
size="1792x1024",
quality="hd",
n=1
)
image_url = response.data[0].url
# Stability AI (local-compatible models)
import requests
resp = requests.post(
"https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image",
headers={"Authorization": f"Bearer {api_key}"},
json={"text_prompts": [{"text": prompt}], "width": 1024, "height": 1024}
)
Robust Error Handling
import openai
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(stop=stop_after_attempt(3), wait=wait_exponential(min=1, max=10))
def generate_text(prompt: str) -> str:
try:
resp = client.chat.completions.create(model="gpt-4o-mini",
messages=[{"role":"user","content":prompt}])
return resp.choices[0].message.content
except openai.RateLimitError:
raise # Let tenacity handle it
except openai.APIError as e:
return f"Generation failed: {str(e)}"
Always implement exponential backoff and fallbacks. At scale, even a 99.9% uptime API will fail you multiple times per day.
Cost Optimisation at Scale
- Cache frequent responses — same prompt, same output, no need to call the API twice.
- Use smaller models for classification and routing, larger ones for generation.
- Compress prompts — reduce system prompt length by 30% with no quality loss using prompt compression.
- Batch requests where latency is not critical — batch APIs are typically 50% cheaper.