> [!NOTE] Quick Start
> 1. Get your API key from [OpenAI Platform](https://platform.openai.com)
> 2. Store it securely (never in plain text or public repos)
> 3. Make API calls using your preferred method
> 4. Monitor usage to stay within rate limits
# OpenAI API Cheat Sheet 🤖
## Understanding Tokens 🎯
> [!INFO] What are Tokens?
> Tokens are the basic units that AI models process. Think of them as pieces of words. A token can be:
> - A single character
> - Part of a word
> - A whole word
> - Or even punctuation
>
> For English text, 1 token ≈ 4 characters or ¾ of a word.
### Examples
| Text | Tokens | Explanation |
|------------------------|--------|---------------------------------------|
| "Hello world!" | 3 | "Hello" (1), "world" (1), "!" (1) |
| "indescribable" | 4 | "in" (1), "des" (1), "cri" (1), "bable" (1) |
| "🌟" | 1 | Single emoji = 1 token |
| "ChatGPT" | 3 | "Chat" (1), "G" (1), "PT" (1) |
### Key Points
1. **Context Window**
- The total tokens a model can process at once
- Includes both input (prompt) and output (response)
- Example: 128K context = 128,000 tokens ≈ 96,000 words
2. **Cost Calculation**
- Input tokens: What you send to the API
- Output tokens: What the model generates
- Different pricing for each
- Example: With GPT-4
```
Input: 100 tokens × $0.03/1K = $0.003
Output: 50 tokens × $0.06/1K = $0.003
Total: = $0.006
```
3. **Common Token Counts**
- 1 page of text ≈ 750 words ≈ 1,000 tokens
- Code tends to use fewer tokens than natural text
- Special tokens like formatting can add overhead
4. **Best Practices**
- Monitor token usage in API responses
- Set maximum token limits for outputs
- Consider compression techniques for long inputs
- Use efficient prompting to save tokens
## Available Models
### O1 Family (Latest)
- **o1-preview**
- Most advanced reasoning capabilities
- Context: 128K tokens (32K output limit)
- Best for: Complex reasoning, math, strategic planning
- Cost: $15/1M input tokens, $60/1M output tokens
- **o1-mini**
- Balanced performance and cost
- Context: 128K tokens
- Best for: General-purpose tasks, speed-critical applications
- Cost: $3/1M input tokens, $12/1M output tokens
### GPT-4 Family
- **GPT-4o** (Optimized)
- High performance, cost-effective
- Context: 128K tokens
- Best for: Extensive multimodal tasks
- Cost: $2.5/1M input tokens, $10/1M output tokens
- **GPT-4o-mini**
- Most cost-efficient 128K model
- Context: 128K tokens
- Best for: Real-time applications, high-volume tasks
- Cost: $0.15/1M input tokens, $0.60/1M output tokens
- **GPT-4 Turbo** (`gpt-4-0125-preview`)
- Latest GPT-4 version
- Context: 128K tokens
- Best for: Complex tasks, reasoning, coding
- Cost: $0.01/1K input tokens, $0.03/1K output tokens
- **GPT-4** (`gpt-4`)
- Stable, reliable
- Context: 8K tokens
- Best for: Production applications
- Cost: $0.03/1K input tokens, $0.06/1K output tokens
### GPT-3.5 Family
- **GPT-3.5 Turbo** (`gpt-3.5-turbo`)
- Fast, cost-effective
- Context: 16K tokens
- Best for: Chat applications, general use
- Cost: $0.0005/1K input tokens, $0.0015/1K output tokens
### Special Purpose
- **Text Embedding Models**
- `text-embedding-3-small`: $0.02/1M tokens
- `text-embedding-3-large`: $0.13/1M tokens
- Best for: Similarity search, clustering
- **DALL·E Models**
- `dall-e-3`: Image generation
- `dall-e-2`: Faster, cheaper alternative
## API Endpoints
### Chat Completions
```bash
POST https://api.openai.com/v1/chat/completions
```
```json
{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"temperature": 0.7,
"max_tokens": 150
}
```
### Embeddings
```bash
POST https://api.openai.com/v1/embeddings
```
```json
{
"model": "text-embedding-3-small",
"input": "Your text here"
}
```
### Image Generation
```bash
POST https://api.openai.com/v1/images/generations
```
```json
{
"model": "dall-e-3",
"prompt": "A cute cat",
"n": 1,
"size": "1024x1024"
}
```
## Common Parameters
### Chat/Completion
- `temperature` (0-2): Higher = more random
- `max_tokens`: Maximum length of response
- `top_p` (0-1): Nucleus sampling threshold
- `frequency_penalty` (-2 to 2): Reduce repetition
- `presence_penalty` (-2 to 2): Encourage new topics
- `stop`: Array of sequences to stop generation
### Images
- `size`: "1024x1024", "512x512", "256x256"
- `response_format`: "url" or "b64_json"
- `quality`: "standard" or "hd"
- `style`: "vivid" or "natural"
## Rate Limits
### Tier-Based Limits
1. **Free Tier**: $100/month
2. **Tier 1**: $5 paid, $100/month
3. **Tier 2**: $50 paid, $500/month
4. **Tier 3**: $100 paid, $1,000/month
5. **Tier 4**: $250 paid, $5,000/month
6. **Tier 5**: $1,000 paid, $200,000/month
### Best Practices
- Implement exponential backoff
- Cache responses when possible
- Monitor usage with headers
- Set user-level rate limits
## Apple Shortcuts Integration
### Basic Setup
1. Create new shortcut
2. Add "Get Contents of URL" action
3. Configure API request:
```
URL: https://api.openai.com/v1/chat/completions
Method: POST
Headers:
Content-Type: application/json
Authorization: Bearer YOUR_API_KEY
```
### Example Shortcuts
#### Text Summarizer
```json
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "Summarize the following text concisely:"
},
{
"role": "user",
"content": "{{input}}"
}
],
"temperature": 0.3,
"max_tokens": 150
}
```
#### Language Translator
```json
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "Translate the following text to {{language}}:"
},
{
"role": "user",
"content": "{{input}}"
}
],
"temperature": 0.3
}
```
#### Writing Assistant
```json
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a writing assistant. Improve the following text while maintaining its meaning:"
},
{
"role": "user",
"content": "{{input}}"
}
],
"temperature": 0.7
}
```
### Shortcuts Tips
1. Store API key in iCloud Keychain
2. Use Dictionary actions to parse JSON
3. Add error handling with If statements
4. Create menus for different functions
5. Use Share Sheet for easy access
## Error Handling
### Common Error Codes
- 401: Invalid API key
- 429: Rate limit exceeded
- 500: Server error
- 503: Service unavailable
### Response Format
```json
{
"error": {
"message": "Error message here",
"type": "invalid_request_error",
"code": "rate_limit_exceeded"
}
}
```
## Security Best Practices
1. **API Key Management**
- Never expose in client-side code
- Use environment variables
- Rotate keys periodically
- Set usage limits
2. **Request Validation**
- Sanitize user input
- Set token limits
- Implement content filtering
- Monitor for abuse
3. **Response Handling**
- Validate response format
- Handle errors gracefully
- Log important events
- Monitor costs
## Related Resources
- [OpenAI API Documentation](https://platform.openai.com/docs)
- [OpenAI Cookbook](https://github.com/openai/openai-cookbook)
- [API Reference](https://platform.openai.com/docs/api-reference)
- [OpenAI Status Page](https://status.openai.com)