> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agents-squads.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Token Economics

> Understand and optimize LLM costs

## Understanding Tokens

Tokens are the unit of LLM pricing. Roughly:

* **1 token ≈ 4 characters** (English)
* **1 token ≈ 0.75 words**
* **100 tokens ≈ 75 words**

## Pricing by Provider

<Tabs>
  <Tab title="Anthropic (Claude)">
    | Model         | Input (per 1M) | Output (per 1M) |
    | ------------- | -------------- | --------------- |
    | Claude Haiku  | \$0.25         | \$1.25          |
    | Claude Sonnet | \$3.00         | \$15.00         |
    | Claude Opus   | \$15.00        | \$75.00         |
  </Tab>

  <Tab title="OpenAI">
    | Model       | Input (per 1M) | Output (per 1M) |
    | ----------- | -------------- | --------------- |
    | GPT-4o-mini | \$0.15         | \$0.60          |
    | GPT-4o      | \$2.50         | \$10.00         |
    | o1          | \$15.00        | \$60.00         |
  </Tab>

  <Tab title="Google (Gemini)">
    | Model        | Input (per 1M) | Output (per 1M) |
    | ------------ | -------------- | --------------- |
    | Gemini Flash | \$0.075        | \$0.30          |
    | Gemini Pro   | \$1.25         | \$5.00          |
    | Gemini Ultra | \$7.00         | \$21.00         |
  </Tab>

  <Tab title="xAI (Grok)">
    | Model       | Input (per 1M) | Output (per 1M) |
    | ----------- | -------------- | --------------- |
    | Grok-2-mini | \$0.10         | \$0.40          |
    | Grok-2      | \$2.00         | \$10.00         |
  </Tab>
</Tabs>

<Note>
  Prices change frequently. Check provider pricing pages for current rates. Output tokens typically cost 3-5x more than input.
</Note>

## Cost Estimation

### Per-Task Estimates

| Task Type               | Est. Tokens      | Est. Cost (Sonnet) |
| ----------------------- | ---------------- | ------------------ |
| Simple Q\&A             | 1K in + 500 out  | \$0.01             |
| Code review             | 10K in + 2K out  | \$0.06             |
| Full implementation     | 50K in + 10K out | \$0.30             |
| Large codebase analysis | 150K in + 5K out | \$0.52             |

### Monthly Projections

```
Light usage (10 tasks/day):
  ~$3-5/month

Medium usage (50 tasks/day):
  ~$15-30/month

Heavy usage (200 tasks/day):
  ~$60-150/month
```

## Optimization Strategies

### 1. Reduce Input Tokens

**Use targeted reads instead of full files:**

```bash theme={null}
# Bad: Read entire codebase
Read **/*.ts

# Good: Query specific patterns
Grep "export function" --type ts
```

**Leverage project config for persistent context:**

<Tabs>
  <Tab title="Claude Code">
    ```markdown theme={null}
    # CLAUDE.md
    Project context that doesn't need repeating each message.
    ```
  </Tab>

  <Tab title="Gemini CLI">
    ```markdown theme={null}
    # GEMINI.md
    Project context that doesn't need repeating each message.
    ```
  </Tab>

  <Tab title="Cursor">
    ```markdown theme={null}
    # .cursorrules
    Project context that doesn't need repeating each message.
    ```
  </Tab>

  <Tab title="OpenCode">
    ```markdown theme={null}
    # AGENTS.md
    Project context that doesn't need repeating each message.
    ```
  </Tab>
</Tabs>

### 2. Reduce Output Tokens

**Request concise responses:**

```markdown theme={null}
Respond in 2-3 sentences. No explanations unless asked.
```

**Use structured output:**

```markdown theme={null}
Return only JSON, no prose:
{"status": "success", "changes": [...]}
```

### 3. Use Appropriate Models

<Tabs>
  <Tab title="Anthropic">
    ```javascript theme={null}
    // Expensive: Using Opus for simple lookup
    const result = await opus.query("What's in package.json?");

    // Optimal: Use Haiku for simple tasks
    const result = await haiku.query("What's in package.json?");
    ```
  </Tab>

  <Tab title="OpenAI">
    ```javascript theme={null}
    // Expensive: Using GPT-4o for simple lookup
    const result = await gpt4o.query("What's in package.json?");

    // Optimal: Use GPT-4o-mini for simple tasks
    const result = await gpt4oMini.query("What's in package.json?");
    ```
  </Tab>

  <Tab title="Google">
    ```javascript theme={null}
    // Expensive: Using Gemini Pro for simple lookup
    const result = await geminiPro.query("What's in package.json?");

    // Optimal: Use Gemini Flash for simple tasks
    const result = await geminiFlash.query("What's in package.json?");
    ```
  </Tab>
</Tabs>

### 4. Cache Expensive Operations

```javascript theme={null}
// Cache research results
const cacheKey = hash(query);
let result = await cache.get(cacheKey);

if (!result) {
  result = await agent.research(query);
  await cache.set(cacheKey, result, TTL);
}
```

### 5. Batch Operations

```javascript theme={null}
// Bad: Many small calls
for (const file of files) {
  await analyze(file);  // 10 API calls
}

// Good: Batch into one call
await analyzeBatch(files);  // 1 API call
```

## Monitoring Costs

### Track Usage

```bash theme={null}
# View recent costs
squads feedback stats

# Check specific agent costs
squads memory show engineering
```

### Set Budgets

Define budget limits in agent configurations:

```markdown theme={null}
# Agent Configuration

## Budget
- Max tokens per task: 50,000
- Max cost per day: $5.00
- Alert threshold: 80%
```

### Cost Alerts

```javascript theme={null}
// Monitor in hooks
if (taskCost > BUDGET_THRESHOLD) {
  notify(`High cost task: $${taskCost}`);
}
```

## Cost-Saving Patterns

### Progressive Enhancement

<Tabs>
  <Tab title="Anthropic">
    ```
    Start: Haiku (cheap, fast)
      ↓
    If insufficient: Sonnet
      ↓
    If still insufficient: Opus
    ```
  </Tab>

  <Tab title="OpenAI">
    ```
    Start: GPT-4o-mini (cheap, fast)
      ↓
    If insufficient: GPT-4o
      ↓
    If still insufficient: o1
    ```
  </Tab>

  <Tab title="Google">
    ```
    Start: Gemini Flash (cheap, fast)
      ↓
    If insufficient: Gemini Pro
      ↓
    If still insufficient: Gemini Ultra
    ```
  </Tab>

  <Tab title="Multi-Provider">
    ```
    Start: Gemini Flash (cheapest)
      ↓
    If insufficient: Claude Sonnet
      ↓
    If still insufficient: Claude Opus / o1
    ```
  </Tab>
</Tabs>

### Summarize Before Processing

```
Large document (100K tokens)
  ↓
Haiku summarizes → Summary (5K tokens)
  ↓
Sonnet processes summary
```

### Parallel with Haiku, Synthesize with Sonnet

<Steps>
  <Step title="Fan out (cheap)">
    Run Task 1, Task 2, Task 3 in parallel with Haiku
  </Step>

  <Step title="Synthesize (quality)">
    Sonnet combines all results into final output
  </Step>
</Steps>

## ROI Framework

### Calculate Value per Token

```
Value = (Time Saved × Hourly Rate) / Tokens Used

Example:
  Task saves 2 hours of dev time
  Dev rate: $100/hour
  Tokens used: 50,000

  Value = ($200) / (50,000 tokens)
  Value = $0.004 per token

  Cost = 50,000 × $0.000003 = $0.15

  ROI = ($200 - $0.15) / $0.15 = 133,233%
```

### When to Optimize

| Scenario                | Priority                     |
| ----------------------- | ---------------------------- |
| High volume, low value  | High - optimize aggressively |
| Low volume, high value  | Low - focus on quality       |
| High volume, high value | Medium - balance both        |
| Low volume, low value   | Question if needed at all    |

## Best Practices

<Check>
  * Monitor costs weekly at minimum
  * Set per-agent and per-squad budgets
  * Use fast/cheap models for high-volume tasks
  * Cache expensive research results
  * Batch similar operations
  * Track ROI, not just costs
</Check>

<Warning>
  **Common cost traps:**

  * Reading entire codebases repeatedly
  * Using premium models for simple tasks
  * Generating verbose explanations
  * Not caching repeated queries
  * Running agents without monitoring
</Warning>

## Related

<CardGroup cols={2}>
  <Card title="Multi-LLM Usage" icon="layer-group" href="/guides/multi-llm">
    Choose the right model for each task
  </Card>

  <Card title="Context Optimization" icon="brain" href="/guides/context-optimization">
    Reduce input token usage
  </Card>
</CardGroup>
