Constants Reference

Source: Amp Code v0.0.1769212917 Master reference for all magic numbers and configuration values.


Table of Contents

  1. Token & Context Constants
  2. Handoff Constants
  3. Tool Limits
  4. Retry & Timeout Constants
  5. Streaming Constants
  6. Course Correction Constants
  7. Cache Control Constants
  8. File System Constants

1. Token & Context Constants

Primary Token Limits

Constant Value Purpose
COMPACTION_THRESHOLD 100,000 Token count triggering summary compaction
MAX_OUTPUT_TOKENS 32,000 Default max output tokens
SUBAGENT_MAX_TOKENS 8,000 Max output for subagents
CHARS_PER_TOKEN 4 Fallback token estimation ratio
BYTES_PER_TOKEN 3.5 Bytes per token for file estimation

Token Estimation

// Characters per token (fallback)
const CHARS_PER_TOKEN = 4;

function estimateTokens(text: string): number {
  return Math.ceil(text.length / CHARS_PER_TOKEN);
}

// File size estimation (for token budgeting)
const BYTES_PER_TOKEN = 3.5;
const MAX_FILE_SIZE_FOR_ESTIMATION = 32768;  // 32KB (conservative for budgeting)

function estimateFileTokens(sizeBytes: number): number {
  const capped = Math.min(sizeBytes, MAX_FILE_SIZE_FOR_ESTIMATION);
  return Math.ceil(capped / BYTES_PER_TOKEN);
}

// Note: Read tool uses a separate limit of 65,536 bytes (64KB)
// The 32KB cap here is intentionally smaller for conservative budgeting

Context Window by Model

Model Family Context Window Max Output Effective Input
Claude Sonnet 4/4.5 1,000,000 32,000 968,000
Claude Opus 4/4.1/4.5 200,000 32,000 168,000
Claude Haiku 4.5 200,000 64,000 136,000
GPT-5/5.1/5.2 400,000 128,000 272,000
Gemini 2.5/3 1,048,576 65,535 983,041

2. Handoff Constants

Constant Value Purpose
HANDOFF_TOKEN_BUDGET 25,000 Max tokens for handoff context
MAX_HANDOFF_FILES 10 Maximum files in handoff
MAX_FILE_SIZE 32,768 Max file size in bytes
HANDOFF_TIMEOUT 30,000 Handoff operation timeout (ms)
FALLBACK_MODEL "vertexai/gemini-2.5-flash" Model for context overflow

Budget Calculation

const HANDOFF_TOKEN_BUDGET = 25000;
const MAX_HANDOFF_FILES = 10;

// Effective per-file budget
const perFileBudget = HANDOFF_TOKEN_BUDGET / MAX_HANDOFF_FILES; // ~2,500 tokens

3. Tool Limits

Output Truncation

Tool Limit Purpose
Bash 50,000 chars Max bash output
Grep 100 matches Max matches returned
Glob 1,000 files Max files returned
MCP 65,536 bytes Max MCP results
Read 2,000 lines Hard cap on returned lines (Read default is 500)
Read 2,000 chars/line Max chars per line

Tool Timeout

Constant Value (ms) Purpose
DEFAULT_TOOL_TIMEOUT 120,000 Default tool timeout
BASH_TIMEOUT 600,000 Bash command timeout
MCP_CALL_TIMEOUT 60,000 MCP tool call timeout

Truncation Implementation

const BASH_OUTPUT_LIMIT = 50000;

function truncateBashOutput(output: string): string {
  if (output.length > BASH_OUTPUT_LIMIT) {
    return output.slice(0, BASH_OUTPUT_LIMIT) +
      `\n\n[Output truncated at ${BASH_OUTPUT_LIMIT} characters]`;
  }
  return output;
}

4. Retry & Timeout Constants

SDK-Level Retry

Constant Value Purpose
MAX_RETRIES 2 Maximum retry attempts
ANTHROPIC_TIMEOUT 600,000ms Anthropic request timeout
OPENAI_TIMEOUT 60,000ms OpenAI request timeout
GOOGLE_TIMEOUT 60,000ms Google request timeout
BASE_RETRY_DELAY 500ms Initial retry delay
MAX_RETRY_DELAY 8,000ms Maximum retry delay

UI-Level Auto-Retry

Constant Value Purpose
BASE_RETRY_SECONDS 5 Initial auto-retry delay
MAX_RETRY_SECONDS 60 Maximum auto-retry delay
MAX_AUTO_RETRIES 5 Maximum auto-retry attempts

Retry Progression

// UI Auto-Retry progression
class RetryHandler {
  static BASE_RETRY_SECONDS = 5;
  static MAX_RETRY_SECONDS = 60;
  static MAX_AUTO_RETRIES = 5;

  getRetryDelaySeconds(attempt: number): number | undefined {
    if (attempt >= RetryHandler.MAX_AUTO_RETRIES) return undefined;
    const delay = RetryHandler.BASE_RETRY_SECONDS * Math.pow(2, attempt);
    return Math.min(delay, RetryHandler.MAX_RETRY_SECONDS);
  }
}

// Progression: 5s → 10s → 20s → 40s → 60s (capped)

5. Streaming Constants

SSE Reconnection

Constant Value Purpose
initialReconnectionDelay 1,000ms Initial reconnect delay
maxReconnectionDelay 30,000ms Maximum reconnect delay
reconnectionDelayGrowFactor 1.5 Exponential growth factor
maxRetries 2 Maximum reconnection attempts

SSE Configuration

const SSE_RECONNECTION_OPTIONS = {
  initialReconnectionDelay: 1000,    // 1 second
  maxReconnectionDelay: 30000,       // 30 seconds
  reconnectionDelayGrowFactor: 1.5,  // 1.5x each retry
  maxRetries: 2                       // 2 retries max
};

// Progression: 1s → 1.5s → 2.25s → ... → 30s (capped)

6. Course Correction Constants

Constant Value Purpose
MIN_TOOL_CALLS 5 Minimum tool calls before check
REQUIRES_FILE_EDIT true Must have file edits
ASSESSMENT_TIMEOUT 1,000ms Wait time for assessment
TEMPERATURE 0.1 Very low for deterministic decisions
MAX_OUTPUT_TOKENS 1,024 Short responses only

Trigger Logic

function shouldRunCourseCorrection(messages: Message[]): boolean {
  let toolCount = 0;
  let hasFileEdits = false;

  for (const msg of getMessagesSinceLastUser(messages)) {
    for (const content of msg.content) {
      if (content.type === "tool_use") {
        toolCount++;
        if (content.name === "edit_file" || content.name === "create_file") {
          hasFileEdits = true;
        }
      }
    }
  }

  // Trigger: 5+ tools AND at least one file edit
  return toolCount >= 5 && hasFileEdits;
}

7. Cache Control Constants

Constant Value Purpose
LAST_MESSAGE_TTL "5m" Cache TTL for last message
BOUNDARY_TTL "1h" Cache TTL for message boundaries
BOUNDARY_INTERVAL 10 Cache marker every N messages

Cache Implementation

function applyCacheControl(messages: Message[]): Message[] {
  if (messages.length === 0) return messages;

  const lastIndex = messages.length - 1;
  const boundaryIndex = Math.floor((messages.length - 1) / 10) * 10;

  return messages.map((msg, i) => {
    if (i === lastIndex) {
      // Last message: 5 minute cache
      return { ...msg, content: addCacheTTL(msg.content, "5m") };
    } else if (i === boundaryIndex - 1 && boundaryIndex > 0) {
      // 10th message boundary: 1 hour cache
      return { ...msg, content: addCacheTTL(msg.content, "1h") };
    }
    return msg;
  });
}

8. File System Constants

Constant Value Purpose
MAX_FILE_SIZE (Read tool) 65,536 Hard limit for Read tool (64KB)
MAX_FILE_SIZE (estimation) 32,768 Cap for token budgeting (32KB)
MAX_READ_LINES 2,000 Default lines to read
MAX_LINE_LENGTH 2,000 Max chars per line
MAX_GLOB_RESULTS 1,000 Max files from glob
MAX_GREP_RESULTS 100 Max grep matches

Note: Two different MAX_FILE_SIZE values exist intentionally. The Read tool accepts files up to 64KB, but token estimation uses a conservative 32KB cap to prevent single files from dominating context budgets.

Guarded File Patterns

const GUARDED_FILE_PATTERNS = [
  "**/.env",
  "**/.env.*",
  "**/.envrc",
  "**/secrets.*",
  "**/credentials.*",
  "**/*_rsa",
  "**/*_dsa",
  "**/*_ed25519",
  "**/*.pem",
  "**/*.key",
  "**/*.p12",
  "**/*.pfx"
];

Summary Tables

All Token Constants

Constant Value Category
Compaction threshold 100,000 Context
Max output tokens 32,000 Output
Subagent max tokens 8,000 Subagent
Handoff token budget 25,000 Handoff
Chars per token 4 Estimation
Bytes per token 3.5 Estimation

All Timeout Constants (ms)

Constant Value Category
Anthropic timeout 600,000 SDK
OpenAI timeout 60,000 SDK
Google timeout 60,000 SDK
Tool timeout 120,000 Tool
Bash timeout 600,000 Tool
MCP timeout 60,000 MCP
Handoff timeout 30,000 Handoff

All Retry Constants

Constant Value Category
SDK max retries 2 SDK
UI max auto-retries 5 UI
SSE max retries 2 Streaming

Constants extracted from v0.0.1769212917