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.
🔵 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:
- Definition —
tools.tsdefinesTOOL_PERMISSIONSas a frozen object mapping tool names to required permission levels. - Barrel Export —
index.tsre-exports all constants from each sub-file viaexport * from "./tools". - Import — The permissions module imports
TOOL_PERMISSIONSfrom"../constants". - Lookup — When the user invokes a tool, the permissions checker looks up the required level in the constant map.
- Prompt Assembly —
prompts.tstemplates reference permission names fromtools.tsto build system prompts. - System Prompt —
system.tsassembles the full system prompt using prompt template sections. - API Limits —
apiLimits.tsvalues are checked before each API call to enforce rate limiting. - Beta Flags —
betas.tsflags are sent as HTTP headers to enable Anthropic beta features.
Key Constants & Exports
| Name | File | Description |
|---|---|---|
TOOL_PERMISSIONS | tools.ts | Map of tool names to their required permission levels (read, write, admin) |
DANGEROUS_TOOLS | tools.ts | Set of tool names that require explicit user confirmation before execution |
SYSTEM_PROMPT_SECTIONS | system.ts | Ordered array of system prompt template sections assembled at startup |
PROMPT_TEMPLATES | prompts.ts | Template strings for various model interactions (summarize, explain, diff) |
MESSAGE_TYPES | messages.ts | Enum of message role types: user, assistant, system, tool_result |
API_RATE_LIMITS | apiLimits.ts | Rate limit thresholds per model tier (requests/min, tokens/min) |
MAX_CONTEXT_TOKENS | apiLimits.ts | Maximum context window sizes for each supported model |
BETA_FEATURES | betas.ts | Anthropic beta feature header strings (e.g., computer-use-2024) |
XML_TAGS | xml.ts | Standard XML tag constants for structured message formatting |
DATE_FORMAT | common.ts | Standard date format strings used across the UI and logs |
DEFAULT_MODEL | common.ts | Default model identifier when none is specified by user |
PERMISSION_MODES | tools.ts | Enum of permission modes: ask, auto-allow, deny |
Constant Categories
| File | Category | Export Count | Description |
|---|---|---|---|
tools.ts | Permissions | ~30 | Tool permission levels, dangerous tool sets, mode enums |
prompts.ts | Prompts | ~25 | Prompt template strings for all model interaction patterns |
messages.ts | Message Rendering System" class="xref">Messages | ~15 | Message type enums, role constants, content type tags |
system.ts | System | ~20 | System prompt sections, persona definitions, capability lists |
apiLimits.ts | API | ~15 | Rate limits, token budgets, retry configuration values |
betas.ts | Betas | ~10 | Anthropic beta feature flags and header strings |
xml.ts | XML | ~20 | XML tag names for structured output formatting |
common.ts | Common | ~15 | Dates, 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);
}