Chat Completions
Basic Chat
Streaming Responses
Real-time streaming for better user experience:React Streaming Example
Image Generation
Generate images with various AI models:Image Generation Parameters
Parameter | Type | Description |
---|---|---|
model | string | AI model to use (required) |
prompt | string | Text description of image (required) |
negativePrompt | string | What to avoid in the image |
size | string | Predefined size like ‘1024x1024’ |
width | number | Custom width in pixels |
height | number | Custom height in pixels |
numImages | number | Number of images to generate |
quality | ’standard’ | ‘hd’ | Image quality setting |
style | ’vivid’ | ‘natural’ | Style preference |
responseFormat | ’url’ | ‘b64_json’ | Response format |
Available Models
Chat Models
- Anthropic:
anthropic/claude-3.5-haiku
,anthropic/claude-sonnet-4
,anthropic/claude-opus-4.1
- OpenAI:
openai/gpt-5
,openai/gpt-5-mini
,openai/gpt-4o
- Google:
google/gemini-2.5-pro
Image Models
- Google:
google/gemini-2.5-flash-image-preview
Chat Application Example
Complete chat interface with conversation history:Image Gallery Example
Error Handling
AI operations return structured errors:Token Usage & Costs
Monitor token usage for cost management:Best Practices
Choose Right Model
Use Haiku for speed, Sonnet for balance, Opus for complex tasks
Stream Long Responses
Use streaming for better UX with long responses
Manage Context
Keep conversation history reasonable to control costs
Handle Errors Gracefully
Always check for errors and provide fallbacks
Cache Responses
Cache common queries to reduce API calls
Validate Images
Check generated images before displaying to users