Inferensys

Prompt

Exit Code Documentation Prompt Template

A practical prompt playbook for using Exit Code Documentation Prompt Template in production AI workflows.
Developer doing prompt engineering on laptop, prompt variations visible on screen, casual coding session.
PROMPT PLAYBOOK

When to Use This Prompt

Understand the job-to-be-done, the ideal user, and the boundaries for the Exit Code Documentation Prompt Template.

This prompt is for CLI maintainers and platform engineers who need to transform raw source code, help text, or internal error catalogs into a structured, user-facing exit code reference. The job-to-be-done is preventing undocumented failure states from reaching users. When a CLI exits with code 137, a user should not have to grep the repository to understand what happened. Use this prompt when you have access to the source code that defines exit points, error-handling logic, or a list of known error strings, and you need a consistent mapping of numeric codes to error categories, typical causes, stderr patterns, and actionable recovery steps. The ideal user is someone who owns the CLI's reliability surface and needs documentation that support engineers and end-users can actually follow.

Do not use this prompt when you lack access to the implementation. If you are documenting a third-party binary with no source code, no --help output, and no error catalog, the model will hallucinate plausible but incorrect exit codes. In that case, you must first run the binary under controlled failure conditions to capture real stderr output and exit codes, then feed those observations into the prompt as [CONTEXT]. Similarly, do not use this prompt for CLIs that do not follow the convention of meaningful exit codes—if every failure exits with code 1 and prints a stack trace, the prompt will produce a thin reference that adds little value. The prompt is also not a replacement for a troubleshooting guide; it documents what each code means, not a decision tree for diagnosing complex failures. For that, use the CLI Exit Code Troubleshooting Guide Prompt instead.

Before running this prompt, gather the required inputs: the source code paths that contain os.Exit, sys.exit, System.exit, or equivalent calls; any internal error-type enumerations; and representative stderr output for each failure mode. The prompt's harness should validate that every exit point found in source produces a documented entry, and that no documented code is unreachable. After generating the reference, wire it into your CI pipeline so that new exit codes introduced in pull requests trigger a documentation drift alert. This closes the loop between code changes and user-facing documentation, which is the core value of this playbook.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Exit Code Documentation Prompt Template delivers reliable structure and where it introduces risk. Use these cards to decide if this prompt fits your current CLI documentation pipeline.

01

Good Fit: Source-Anchored Codebases

Use when: You have access to the source code, and exit codes are defined as constants, enums, or structured error factories. The prompt excels at mapping code paths to human-readable documentation. Guardrail: Always pair the prompt with a static analysis harness that extracts actual exit codes from source to validate completeness.

02

Bad Fit: Undocumented Legacy Binaries

Avoid when: The CLI is a black-box binary with no source access and no structured error output. The model will hallucinate plausible but incorrect exit codes. Guardrail: If source is unavailable, switch to a dynamic analysis approach that triggers errors and captures actual exit codes before attempting documentation.

03

Required Inputs

Risk: Incomplete inputs produce a reference that looks authoritative but misses critical error paths. Guardrail: Require at minimum: (1) source code or error-handling logic, (2) a list of all possible exit codes, and (3) stderr patterns for each code. Without all three, flag the output as 'draft-incomplete' and block publication.

04

Operational Risk: Silent Drift

Risk: Developers add new exit codes or change error messages without updating the documentation. The reference becomes stale and misleading. Guardrail: Integrate this prompt into a CI pipeline that runs on every release. A drift detection harness must compare documented codes against extracted codes and fail the build on mismatches.

05

Operational Risk: Recovery Instruction Decay

Risk: The prompt generates recovery steps that made sense at documentation time but become incorrect after dependency or infrastructure changes. Guardrail: Treat recovery instructions as testable assertions. A validation harness should execute documented recovery commands in a sandbox and verify they resolve the intended error condition.

06

Boundary: Platform-Specific Exit Codes

Risk: The model may document exit codes from the operating system or shell (e.g., signal-based codes like 128+SIGTERM) as if they are application-specific. Guardrail: Constrain the prompt to only document codes explicitly defined in the application source. Add a separate section for OS-level codes with a clear disclaimer that they are not owned by the CLI tool.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt template for generating a structured exit code reference from source code, help text, or design specs.

This prompt template is designed to produce a complete, structured exit code reference for a CLI tool. It maps every possible numeric exit code to its error category, typical causes, stderr patterns, and recommended user recovery actions. The template uses square-bracket placeholders that you must replace with your project-specific values before running. The goal is to create documentation that is directly usable by operators, support engineers, and automated monitoring systems.

text
You are a technical writer specializing in CLI tool documentation. Your task is to generate a complete exit code reference for a command-line tool.

## Input
- Source code or binary path: [SOURCE_CODE_PATH_OR_BINARY]
- Help text output: [HELP_TEXT_OUTPUT]
- Existing documentation (if any): [EXISTING_DOCS]
- Target audience: [AUDIENCE] (e.g., SRE, end-user, support engineer)

## Constraints
- Document every exit code the tool can produce. Do not skip codes reserved by the OS or language runtime unless they are explicitly handled by the tool.
- For each exit code, provide: numeric code, symbolic name (if any), error category, typical causes, a representative stderr pattern, and a step-by-step recovery action for the user.
- If an exit code is undocumented in the source, flag it as "Undocumented in source" and infer its meaning from context.
- Use clear, imperative language for recovery actions.
- [ADDITIONAL_CONSTRAINTS]

## Output Schema
Return a JSON object with the following structure:
{
  "tool_name": "string",
  "version": "string",
  "exit_codes": [
    {
      "code": 0,
      "symbolic_name": "SUCCESS",
      "category": "Success",
      "description": "The command completed successfully.",
      "typical_causes": ["All operations finished without error."],
      "stderr_pattern": null,
      "recovery_steps": ["No action required."]
    },
    {
      "code": 1,
      "symbolic_name": "GENERAL_ERROR",
      "category": "Runtime Error",
      "description": "A general, non-specific error occurred.",
      "typical_causes": ["Invalid command syntax", "Missing required argument", "File not found"],
      "stderr_pattern": "error: .*",
      "recovery_steps": ["Check the command syntax using --help.", "Verify all required arguments are provided.", "Ensure input files exist and are readable."]
    }
  ],
  "undocumented_codes": [
    {
      "code": 130,
      "inferred_meaning": "Terminated by SIGINT (Ctrl+C)",
      "source_location": "Not explicitly handled; default OS behavior"
    }
  ]
}

## Examples
[FEW_SHOT_EXAMPLES]

## Risk Level
[RISK_LEVEL] (If 'high', require human review of all recovery steps before publication.)

To adapt this template, start by replacing [SOURCE_CODE_PATH_OR_BINARY] with a pointer to your codebase or a path to the compiled binary. Provide the raw output of your-cli-tool --help for [HELP_TEXT_OUTPUT]. Use [ADDITIONAL_CONSTRAINTS] to enforce style guide rules, such as "Recovery steps must not require root access" or "Do not document internal debug codes." The [FEW_SHOT_EXAMPLES] placeholder is critical for teaching the model the exact tone and level of detail you expect; include at least two well-crafted exit code entries as examples. After generation, always validate the output against the source code to catch missing or hallucinated codes before publishing.

IMPLEMENTATION TABLE

Prompt Variables

Inputs required for the Exit Code Documentation Prompt Template. Provide these to generate a structured exit code reference from source code analysis.

PlaceholderPurposeExampleValidation Notes

[SOURCE_CODE_PATH]

Path to the source file or directory containing exit code definitions

src/main.c or src/errors/

Must be a valid filesystem path accessible to the harness. Directory inputs trigger recursive scan for exit() calls and error enums.

[LANGUAGE]

Programming language of the source code for parser selection

C, Go, Rust, Python

Must match one of the supported parser backends. Mismatch causes parse failure. Validate against file extensions in [SOURCE_CODE_PATH].

[EXIT_CODE_PATTERN]

Regex or AST pattern identifying exit code assignments in source

exit((\d+)) or os.Exit((\d+))

Pattern must compile without errors. Test against known exit calls in a sample file before full run. Null allowed if using default language patterns.

[STDERR_CATALOG_PATH]

Optional path to a file mapping exit codes to known stderr message templates

errors.yaml or null

If provided, must be valid YAML/JSON with code-to-message mapping. Null triggers extraction of stderr strings from source directly. Schema: {code: string}.

[OUTPUT_FORMAT]

Desired format for the exit code reference document

markdown, json, man

Must be one of the enumerated values. Harness selects the appropriate output template. Invalid values cause a schema rejection before model call.

[INCLUDE_RECOVERY_ACTIONS]

Boolean flag to generate user-facing recovery steps for each exit code

Must be true or false. When true, the prompt adds a Recovery section per code. When false, only cause and stderr pattern are documented.

[MAX_CODE_VALUE]

Upper bound for exit code enumeration to prevent unbounded loops in analysis

255

Must be an integer between 1 and 255. Used to validate that all discovered codes fall within the expected range. Codes outside this range trigger a warning in the harness.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the exit code documentation prompt into a CI pipeline or documentation build system with validation, retries, and coverage checks.

The exit code documentation prompt is designed to run as part of a documentation build pipeline, not as a one-off manual query. The harness should invoke the prompt once per CLI binary or source tree, passing extracted exit code paths as [CODE_PATHS] and any existing documentation as [CURRENT_DOCS] for diff-aware updates. The model should receive the full list of code paths discovered by static analysis (e.g., grep for os.Exit, sys.exit, return codes in main functions) so it can flag undocumented exits rather than hallucinating coverage. Run this prompt after every release that changes error handling, and treat the output as a pull request against your docs repository.

Validation and retry logic is critical because exit code documentation errors mislead operators during incidents. After the model returns a structured exit code reference, run a coverage validator that parses the output table and compares numeric codes against the source-derived [CODE_PATHS] list. Flag any code present in source but missing from the documentation as a COVERAGE_GAP, and any code in the documentation but absent from source as a STALE_ENTRY. If coverage gaps exceed a threshold (e.g., >0 undocumented exit codes), trigger a retry with explicit feedback: append the missing codes to the prompt as [MISSING_CODES] and request a targeted update. For high-reliability CLIs, add a human review gate that requires a maintainer to approve any exit code whose recovery_action field recommends destructive operations (e.g., rm -rf, database resets, or credential rotation). Log every generation with the source commit SHA, the model version, the coverage percentage, and the reviewer identity for auditability.

Model choice and tool integration should favor models with strong structured output support (GPT-4o, Claude 3.5 Sonnet, or equivalent) and JSON mode enabled. The prompt expects a schema with fields like code, category, cause, stderr_pattern, and recovery_action—enforce this with your model's structured output API rather than hoping the model follows a markdown table. Wire the harness into your existing docs toolchain: if you use a static site generator, the output should render as a dedicated exit codes page with anchor links; if you ship man pages, post-process the output into the EXIT STATUS section. Avoid running this prompt on every commit—trigger it on release tags or when grep detects changes to exit-relevant code paths. The harness should also compare generated stderr_pattern fields against actual error output from integration tests to catch drift between documented and real error messages.

IMPLEMENTATION TABLE

Expected Output Contract

Define the exact shape of the exit code reference document. Use this contract to validate the model's output before publishing.

Field or ElementType or FormatRequiredValidation Rule

exit_codes

Array of objects

Array must not be empty. Each element must match the exit_code_object schema.

exit_code_object.code

Integer (0-255)

Must be a unique integer within the array. Check against source code for missing codes.

exit_code_object.category

Enum string

Must be one of: [SUCCESS, USER_ERROR, SYSTEM_ERROR, PERMISSION, TIMEOUT, UNKNOWN]. Validate enum membership.

exit_code_object.cause

String

Must be non-empty. Should describe the root condition, not just the symptom. Human review required for accuracy.

exit_code_object.stderr_pattern

String or null

If provided, must be a valid regex string. Test regex against actual error output samples.

exit_code_object.recovery_action

String

Must be an imperative, actionable instruction. Validate that the action is executable and resolves the documented cause.

exit_code_object.related_flags

Array of strings or null

If provided, each string must match a documented CLI flag name. Cross-reference with the flag reference schema.

exit_code_object.retry_allowed

Boolean

Must be true or false. If true, the recovery_action must describe an idempotent or safe retry strategy.

PRACTICAL GUARDRAILS

Common Failure Modes

Exit code documentation fails when it drifts from source, misses error paths, or provides unhelpful recovery steps. These cards cover the most common failure modes and how to guard against them before users encounter broken references.

01

Undocumented Exit Codes from Source

What to watch: The prompt generates documentation from a static list or help text, but the source code contains exit paths with codes that never appear in the documented catalog. Users encounter mystery exit codes with no explanation. Guardrail: Run a static analysis pass over the source to extract all sys.exit(), process.exit(), and exit() calls, then diff the extracted set against the documented codes. Flag any code present in source but missing from documentation before publishing.

02

Stale Recovery Instructions After Code Changes

What to watch: The documented recovery action for exit code 4 says 'check your config file at ~/.config/app.yaml,' but a recent refactor moved config to ~/.local/share/app/config.toml. The exit code is still correct, but the recovery path is wrong. Guardrail: Pair each documented recovery step with a test that executes the recommended action against the current binary. If the recovery command fails or references a nonexistent path, block the documentation update and flag the specific step for revision.

03

Missing Error Categories for Partial Failures

What to watch: The prompt documents exit codes for clean success (0) and hard failures (1-10), but misses codes for partial success states—like '3 files processed, 2 skipped due to permissions.' Users can't distinguish between 'nothing worked' and 'some things worked.' Guardrail: Require the harness to check for exit codes in the 2-99 range that indicate partial completion, warnings, or degraded operation. If the source emits these codes but documentation treats them as generic errors, flag the gap and request category refinement.

04

Inconsistent stderr Pattern Documentation

What to watch: The prompt documents exit code 2 with a stderr pattern like 'Error: file not found,' but the actual binary emits 'ERROR [E002] FileNotFound: /path/to/file' in a structured log format. Users grep for the documented string and find nothing. Guardrail: Validate documented stderr patterns by running the binary in failure mode, capturing actual stderr output, and checking that the documented pattern appears as a substring match. If no match, flag the pattern as stale and suggest the actual output.

05

Exit Code Collision Across Subcommands

What to watch: The prompt documents exit code 3 as 'invalid input' for the top-level command, but the build subcommand also uses exit code 3 for 'dependency resolution failure.' The flat reference conflates unrelated error conditions. Guardrail: Scope exit code documentation per subcommand and validate that each code has a unique meaning within its scope. If the same numeric code maps to different causes across subcommands, require separate entries with explicit subcommand context in the reference.

06

Unhelpful Recovery Actions That Repeat the Error

What to watch: The prompt generates recovery text like 'If you see exit code 5, check your permissions and try again.' The user checks permissions, they're correct, and retrying produces the same exit code. The recovery action is circular. Guardrail: For each documented recovery step, require a concrete diagnostic command that produces actionable information (e.g., stat --format=%A /path or id -Gn). If the recovery step contains only vague advice without a verifiable check, flag it as insufficient and request a specific diagnostic.

IMPLEMENTATION TABLE

Evaluation Rubric

Use this rubric to test the exit code documentation prompt before shipping. Each criterion targets a specific failure mode common in CLI reference generation.

CriterionPass StandardFailure SignalTest Method

Exit Code Coverage

Every exit code in the source code or binary introspection appears in the output with a numeric code and category

Documented exit codes are fewer than codes found in source; undocumented codes appear in production

Diff the documented exit code list against a static analysis extraction of all exit() or sys.exit() calls in the target CLI source

Cause Accuracy

Each documented cause matches the actual code path that produces the exit code

A documented cause describes a condition that never triggers that exit code; cause contradicts the source logic

For each exit code, trace one documented cause to its source location and verify the condition matches

Recovery Action Validity

Every recovery action is executable by the user and resolves the documented cause

Recovery action references a flag that doesn't exist, a command that fails, or a file path the user cannot access

Execute each recovery action in a test environment with the error condition intentionally triggered; confirm exit code 0 after recovery

Stderr Pattern Match

Documented stderr patterns match actual error output strings from the CLI

Stderr pattern is a regex that never matches actual output; pattern is too specific and breaks on minor wording changes

Capture actual stderr for each exit code and test the documented pattern against it using grep or a regex validator

Category Consistency

All exit codes are assigned to exactly one error category, and categories are mutually exclusive

Same exit code appears in two categories; a category contains codes with unrelated causes

Build a category-to-code mapping and check for duplicate code assignments; review category definitions for overlap

Non-Zero Coverage Only

Exit code 0 is documented only if it has meaningful variants; otherwise the reference focuses on error codes 1-255

Exit code 0 has a verbose entry duplicating the success path; error codes are missing while success is over-documented

Count documented codes; flag if code 0 entry exceeds 20% of total content or if any non-zero code from source is absent

Shell Redirection Behavior

Each exit code entry notes whether the code propagates through pipes, is masked by set -e, or triggers ERR traps

Documentation omits shell behavior; user writes a pipeline that silently ignores a critical exit code

Test each documented exit code in a pipeline (cmd || echo 'failed') and verify the documented propagation behavior matches actual shell semantics

Cross-Reference Integrity

Every see-also link or related-code reference points to an existing, correct exit code entry

Cross-reference links to a code not documented; circular references between two entries with no resolution path

Parse all cross-references and verify each target exists in the output; check for reference chains longer than 2 hops

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a single source file and lighter validation. Replace the full harness with a manual spot check: run the prompt, compare exit codes against grep -r 'exit(' [SOURCE], and flag any numeric code in source that doesn't appear in the output.

Watch for

  • Exit codes hidden behind macros or constants that grep won't catch
  • Codes returned by library calls rather than explicit exit() or sys.exit()
  • Missing recovery actions when the model only documents the code number and description
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.