constants — Centralized Constant Definitions

Centralized Constant Definitions

Global constants imported across the entire Claude Code codebase. Houses permission constants, prompt templates, message type definitions, system prompt sections, API rate limit thresholds, Anthropic beta feature flags, XML tag constants, and common formatting values.

21
Files
8
Categories
200+
Exports
TS
Language

🔵 Global Constants 🟢 Prompt Templates 🟡 API Limits 🔴 Permission Defs

💡 What is this?

The constants module is the single source of truth for all magic values, string literals, and configuration constants used across Claude Code. Instead of scattering string literals throughout the codebase, every module imports from here.

🧩 Analogy: Think of it like a data dictionary

Just as a database schema defines the official names and types for every column, the constants module defines the official values for every permission, prompt template, and API limit. If you need to change a rate limit or update a system prompt, you change it once here and every consumer gets the updated value.

Architecture

flowchart LR
  subgraph Constants["constants/"]
    T["tools.ts
Permission Constants"] P["prompts.ts
Prompt Templates"] M["messages.ts
Message Types"] S["system.ts
System Prompt"] A["apiLimits.ts
Rate Limits"] B["betas.ts
Beta Features"] X["xml.ts
XML Tags"] C["common.ts
Common Values"] end subgraph Consumers["Consumer Modules"] PERM["permissions/"] AGENT["agent/"] API["api/"] REPL["repl/"] end T --> PERM P --> AGENT S --> AGENT M --> REPL A --> API B --> API X --> AGENT C --> REPL

Data Flow

stateDiagram-v2
  [*] --> SourceFiles: TypeScript compilation
  SourceFiles --> BarrelExport: index.ts re-exports
  BarrelExport --> TreeShaken: Bundler tree-shakes
  TreeShaken --> Runtime: Only used constants included
  state SourceFiles {
    tools --> prompts
    prompts --> system
    system --> messages
    apiLimits --> betas
  }

🚶 Step-by-Step Narrated Walkthrough

Follow how a permission check constant is defined and consumed:

  1. Definition — tools.ts defines TOOL_PERMISSIONS as a frozen object mapping tool names to required permission levels.
  2. Barrel Export — index.ts re-exports all constants from each sub-file via export * from "./tools".
  3. Import — The permissions module imports TOOL_PERMISSIONS from "../constants".
  4. Lookup — When the user invokes a tool, the permissions checker looks up the required level in the constant map.
  5. Prompt Assembly — prompts.ts templates reference permission names from tools.ts to build system prompts.
  6. System Prompt — system.ts assembles the full system prompt using prompt template sections.
  7. API Limits — apiLimits.ts values are checked before each API call to enforce rate limiting.
  8. Beta Flags — betas.ts flags are sent as HTTP headers to enable Anthropic beta features.

Key Constants & Exports

NameFileDescription
TOOL_PERMISSIONStools.tsMap of tool names to their required permission levels (read, write, admin)
DANGEROUS_TOOLStools.tsSet of tool names that require explicit user confirmation before execution
SYSTEM_PROMPT_SECTIONSsystem.tsOrdered array of system prompt template sections assembled at startup
PROMPT_TEMPLATESprompts.tsTemplate strings for various model interactions (summarize, explain, diff)
MESSAGE_TYPESmessages.tsEnum of message role types: user, assistant, system, tool_result
API_RATE_LIMITSapiLimits.tsRate limit thresholds per model tier (requests/min, tokens/min)
MAX_CONTEXT_TOKENSapiLimits.tsMaximum context window sizes for each supported model
BETA_FEATURESbetas.tsAnthropic beta feature header strings (e.g., computer-use-2024)
XML_TAGSxml.tsStandard XML tag constants for structured message formatting
DATE_FORMATcommon.tsStandard date format strings used across the UI and logs
DEFAULT_MODELcommon.tsDefault model identifier when none is specified by user
PERMISSION_MODEStools.tsEnum of permission modes: ask, auto-allow, deny

Constant Categories

FileCategoryExport CountDescription
tools.tsPermissions~30Tool permission levels, dangerous tool sets, mode enums
prompts.tsPrompts~25Prompt template strings for all model interaction patterns
messages.tsMessage Rendering System" class="xref">Messages~15Message type enums, role constants, content type tags
system.tsSystem~20System prompt sections, persona definitions, capability lists
apiLimits.tsAPI~15Rate limits, token budgets, retry configuration values
betas.tsBetas~10Anthropic beta feature flags and header strings
xml.tsXML~20XML tag names for structured output formatting
common.tsCommon~15Dates, formatting, default model, version strings

Directory Map

Source tree for constants
constants/
├── index.ts                   ← Barrel export (re-exports all)
├── tools.ts                   ← Permission constants & tool flags
├── prompts.ts                 ← Prompt template strings
├── messages.ts                ← Message type enums
├── system.ts                  ← System prompt section definitions
├── apiLimits.ts               ← Rate limit thresholds & token budgets
├── betas.ts                   ← Anthropic beta feature flags
├── xml.ts                     ← XML tag name constants
├── common.ts                  ← Dates, formatting, defaults
├── errors.ts                  ← Error message string constants
├── colors.ts                  ← Terminal color/ANSI constants
├── paths.ts                   ← File system path constants
├── commands.ts                ← Slash command name constants
├── keycodes.ts                ← Key code mapping constants
├── models.ts                  ← Model identifier strings
├── env.ts                     ← Environment variable names
├── __tests__/
│   ├── tools.test.ts          ← Permission constant tests
│   ├── apiLimits.test.ts      ← Rate limit validation tests
│   ├── prompts.test.ts        ← Prompt template tests
│   └── system.test.ts         ← System prompt assembly tests
└── README.md                  ← Module documentation

Code Example

import {
  TOOL_PERMISSIONS,
  API_RATE_LIMITS,
  SYSTEM_PROMPT_SECTIONS,
  BETA_FEATURES,
  XML_TAGS,
} from "../constants";

// Check tool permission level
const level = TOOL_PERMISSIONS["bash"];
if (level === "admin") {
  await requestUserConfirmation();
}

// Build system prompt from sections
const systemPrompt = SYSTEM_PROMPT_SECTIONS
  .map((section) => section.content)
  .join("\n\n");

// Check rate limits before API call
const limit = API_RATE_LIMITS["claude-sonnet"];
if (currentRequests >= limit.requestsPerMinute) {
  await waitForRateLimit(limit);
}