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

  1. Visit Google AI Studio
  2. Log in with Google account
  3. Click “Create API Key”
  4. Select project (existing or new)
  5. 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

ModelCharacteristicsCostApplicable Scenario
gemini-flash-latestFastestLowDaily conversation, production preferred
gemini-proMore capableMediumComplex reasoning, code generation
gemini-2.0-flash-expExperimental, more capableVariableTesting 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 TypeDefaultDescription
Requests per day1500Max requests per day
Requests per minute60Max requests per minute
Tokens per day1,000,000Max tokens per day

What if quota runs out:

  1. Apply for quota increase in Google AI Studio
  2. Design reasonable caching strategy, reduce duplicate requests
  3. 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 实战干货.