API Key & Model Selection: Balancing Cost, Capability, and Use Case
Table of Contents
API Key & Model Selection: Balancing Cost, Capability, and Use Case
Developing apps with ADK Go, two issues are unavoidable: how to manage API Key, and which of the many models to use. This article helps you clarify both.
Google Gemini API Key
Get a Key
- Visit Google AI Studio
- Log in with Google account
- Click “Create API Key”
- Select project (existing or new)
- Copy the generated Key
Managing Keys
Principle: Never commit real Key to code repository.
Common approaches:
Method 1: .env file (Recommended for team development)
.env.example (committed)
export GOOGLE_API_KEY="your-api-key-here"
.env (not committed)
export GOOGLE_API_KEY="actual-key-value"
Team members each maintain their own .env, .gitignore excludes .env.
Method 2: Direct environment variable (Quick verification)
export GOOGLE_API_KEY="your-key"
go run agent.go
Method 3: Secret Manager (Recommended for production)
In production, don’t store in .env file, use cloud Secret Manager:
- Google Cloud Secret Manager
- AWS Secrets Manager
- Azure Key Vault
Read in code:
import "cloud.google.com/go/secretmanager"
func getAPIKey() string {
ctx := context.Background()
client, _ := secretmanager.NewClient(ctx)
defer client.Close()
result, _ := client.AccessSecretVersion(ctx, &secretmanagerpb.AccessSecretVersionRequest{
Name: "projects/my-project/secrets/gemini-api-key/versions/latest",
})
return string(result.Payload.Data)
}
Model Selection: Flash vs Pro vs Experimental
Model Overview
| Model | Characteristics | Cost | Applicable Scenario |
|---|---|---|---|
gemini-flash-latest | Fastest | Low | Daily conversation, production preferred |
gemini-pro | More capable | Medium | Complex reasoning, code generation |
gemini-2.0-flash-exp | Experimental, more capable | Variable | Testing new features |
Speed vs Capability
gemini-flash-latest ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ Fast speed, moderate capability
gemini-pro ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ Slow speed, high capability
gemini-2.0-flash-exp ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ Fast speed, high capability (experimental)
Cost Control Suggestions
Daily development: Use Flash
Flash cheapest, sufficient for daily debugging. In many scenarios Flash capability already far exceeds “usable”.
When capability needed: Upgrade to Pro
When you find Flash performs insufficiently on complex reasoning tasks (e.g., multi-step math, code debugging), upgrade to Pro.
Experimental features: Use Experimental
gemini-2.0-flash-exp and other experimental versions have latest features, good for testing, not suitable for production.
Switch Models in Code
// Development environment
model, _ := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{
APIKey: os.Getenv("GOOGLE_API_KEY"),
})
// Production (switch as needed)
modelName := os.Getenv("GEMINI_MODEL") // Read from environment variable
model, _ := gemini.NewModel(ctx, modelName, &genai.ClientConfig{
APIKey: os.Getenv("GOOGLE_API_KEY"),
})
Quotas and Limits
API Quotas
Each Gemini API Key has default quota limits:
| Quota Type | Default | Description |
|---|---|---|
| Requests per day | 1500 | Max requests per day |
| Requests per minute | 60 | Max requests per minute |
| Tokens per day | 1,000,000 | Max tokens per day |
What if quota runs out:
- Apply for quota increase in Google AI Studio
- Design reasonable caching strategy, reduce duplicate requests
- Use during off-peak hours
Over-Quota Errors
429 RESOURCE_EXHAUSTED: Quota exceeded for quota group 'default'
When encountering this error, quota is exhausted. Need to wait for quota reset or apply for increase in AI Studio.
Multi-Key Polling: Load and Backup
Production environment recommends using multiple API Keys, polling, dispersing quota pressure:
type KeyPool struct {
keys []string
index int
mu sync.Mutex
}
func (p *KeyPool) GetKey() string {
p.mu.Lock()
defer p.mu.Unlock()
key := p.keys[p.index]
p.index = (p.index + 1) % len(p.keys)
return key
}
func main() {
pool := &KeyPool{
keys: []string{
os.Getenv("GOOGLE_API_KEY_1"),
os.Getenv("GOOGLE_API_KEY_2"),
},
}
model, _ := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{
APIKey: pool.GetKey(), // Auto-poll each request
})
}
Common Issues
Q: Works fine locally, but UNAUTHORIZED when deployed to server
A: Check if server region can access Google API. Mainland China needs proxy.
Q: Same Key sometimes works, sometimes reports quota insufficient A: Quota resets by time window, easy to exceed during peak hours. Implement Key polling or multi-level caching.
Q: Experimental model suddenly unavailable A: Experimental models can be taken offline or replaced by Google anytime, don’t depend on them in production.
Next Steps
Module 2’s four articles complete. Next, Module 3: Tool Usage, learning how to customize Tools so Agent truly “gets things done”.
← CLI vs Web | Function Tool Basics →
Follow “Mengshou Programming” on WeChat for more Go ADK hands-on tutorials, weekly updates on Go / AI programming 实战干货.
