esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

Build a Small CLI Tool

How to build a small command-line tool that is safe to run, with flags, a dry run, clear errors and exit codes, for your AI interview.

In short

A small CLI is a program you run from a terminal with a few typed options. Build it to check its inputs, preview risky steps with a dry run, and end with an exit code that tells the truth.

11 min readBeginnerOccasional · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

A small CLI is a program you run from a terminal with a few typed options. I build one by checking every input before any work starts. Anything that writes or sends gets a dry run first, which prints the plan and changes nothing. Errors go to the error stream, and the program ends with an exit code. Zero means it worked. Any other number means it failed, so whatever runs the job can alert someone.

What is a small CLI tool, in plain words?

A CLI, or command-line interface, is a program you run from a terminal. You type its name and a few options, it does one job, prints what it did and says whether it worked. There is no window and no button.

Picture a night-shift records clerk. The ward manager gives them a fixed checklist: the same steps every night, the same kind of input, and a clear yes or no at the end. A good CLI works the same way. It is quiet when all is well and clear when something is wrong.

Here is the real case. A claims support team wants escalated tickets sent to their claims system each morning. An engineer runs a script by hand, once. Two months later a scheduler (the tool that starts a job at set times) runs it every night, and people who never saw the code depend on it. That is when it has to become a proper tool.

Key words

CLI
A program you run from a terminal, with typed options.
Flag
An option you type, like --dry-run. It changes what the program does.
Dry run
A preview. The program prints what it would do and changes nothing.
Exit code
A number the program hands back when it ends. Zero means it worked.
Error stream
The separate output (stderr) for error messages, apart from normal output.

What should a tool check before it does anything?

Three common causes of CLI failures are a bad value nobody checked, a risky step with no preview, and a failure that looks like a success. Design against all three before you write the job itself.

  • Input

    Check it first

    Validate every flag and file path before any work starts. A bad value stops the run with a clear message, and nothing is changed.

  • Action

    Preview the risky step

    Anything that writes or sends gets a --dry-run flag. It prints the plan and changes nothing.

  • Outcome

    Fail loudly

    A failed run that looks like a success is the worst case. The claims are missing and nobody knows why.

One run of the export tool

You type the command

one line in a terminal

Check the input

Read the flags

bad values stop it here

Do the work

Dry run?

yes: print the plan only

Send tickets, if not dry

to the claims system

Exit code

0 worked, 1 if no tickets matched

  1. 1You type the command and its flags.
  2. 2Good flags pass on to the dry-run check.
  3. 3Not a dry run: the tickets are sent.
  4. 4A run that sends tickets ends with exit code 0.
  5. 5A dry run that finds tickets also ends with exit code 0, having sent nothing.
One run of the tool, read left to right. A bad flag stops it before any work, and a dry run prints the plan and sends nothing.

The tool we will look at exports escalated tickets from a made-up queue. It has a priority filter and a dry run, and it ends with an exit code.

Tip

Asked how you would build one? Name the failure cases first, then the normal run.

How do you read the flags and show help?

Python has a built-in module for this called argparse. It reads the flags, checks their types, and builds the help screen from the text you give each flag.

The code below is the whole tool, 29 lines. Save it as export_tickets.py and run it with python3 export_tickets.py --min-priority 2 --dry-run. The output under the code is what it printed.

Python
import argparse
import sys

# Made-up escalation queue: (ticket, priority where 1 is most urgent, owning team, issue)
QUEUE = [
    ("T-2041", 1, "Payments", "Refund stuck after bank timeout"),
    ("T-2042", 3, "Onboarding", "Address change not saved"),
    ("T-2043", 2, "Claims", "Photo upload fails on mobile app"),
    ("T-2044", 1, "Claims", "Claim paid twice, same policy"),
]

def main(argv):
    parser = argparse.ArgumentParser(description="Export escalated support tickets.")
    parser.add_argument("--min-priority", type=int, default=2,
                        help="include tickets up to this priority (1 is most urgent)")
    parser.add_argument("--dry-run", action="store_true",
                        help="show what would be sent, send nothing")
    args = parser.parse_args(argv)
    picked = [t for t in QUEUE if t[1] <= args.min_priority]
    if not picked:
        print("No tickets match, nothing exported.", file=sys.stderr)
        return 1
    verb = "would send" if args.dry_run else "sent"
    for ticket, priority, team, issue in picked:
        print(f"{verb} {ticket} (P{priority}) to {team}: {issue}")
    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

Output

would send T-2041 (P1) to Payments: Refund stuck after bank timeout
would send T-2043 (P2) to Claims: Photo upload fails on mobile app
would send T-2044 (P1) to Claims: Claim paid twice, same policy

A flag with a bad value stops before any work. The argparse module also generates help for you, so running the command with --help shows the manual for free. Each flag's help text is what your teammate reads at night, so write it in plain words.

Bigger tools split their work into subcommands. The Python docs use the svn program as an example, with checkout and commit as its subcommands, and each subcommand has its own flags.

How do exit codes and error messages work?

When a program ends, it hands the system one number. Python's sys.exit does this. Passing it any other object, such as a message, prints that message to the error stream, and the exit code becomes 1.

  • “If it is an integer, zero is considered "successful termination" and any nonzero value is considered "abnormal termination" by shells and the like.”

    Python documentation, sys.exit

  • “Unix programs generally use 2 for command line syntax errors and 1 for all other kinds of errors.”

    Python documentation, sys.exit

The number is the handoff. A scheduler, or a CI pipeline (the automatic build and test runner), reads it and decides whether the job passed or needs a person. A program that prints done and exits with zero after half its tickets failed has broken that handoff.

What the scheduler does with the exit code

Your tool

runs every night at 06:00

Exit code

0 worked, other numbers failed

Scheduler reads it

Code 0

job counts as done

Any code but 0

a person can be alerted

  1. 1The tool finishes and hands over one number.
  2. 2Zero: the scheduler marks the run as done.
  3. 3Any other number: the scheduler can alert the support lead.
The tool reports one number when it finishes. The scheduler reads that number and decides whether the run passed or needs a person.

Errors go to the error stream, and the exit code tells the truth.

How would you ship this tool to a team that didn't write it?

A forward deployed engineer, someone who builds small tools inside a customer's team, would ship this to a claims team. The team will run it every night and never open the code, so the tool has to explain itself.

  1. 1

    Write the help text for a tired reader

    Say what each flag does in plain words. The help screen is the manual they will use at six in the morning.

  2. 2

    Test the parser and the exit codes

    Write tests that run the tool with a bad flag and with an empty queue. Python's unittest module, from the standard library, runs them.

  3. 3

    Keep keys out of the code

    Read the API key, the password a service gives your program, from an environment variable (a setting the computer passes in) or a secret store (a locked vault for passwords). Never type it into a file in the project.

  4. 4

    Dry run for the first week

    Run with --dry-run until the output matches what the team expects, then turn the sending on.

  5. 5

    Name who gets the alert

    Write down who is told when the exit code is not zero, and what they should do first.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: a program you run from a terminal, with typed options.
  • Check the inputs first, and stop before any work when they are wrong.
  • A dry run for anything that writes or sends.
  • Errors to the error stream, and an exit code that tells the truth.
  • Help text and tests, so someone else can run the tool.
  • Keys from the environment, never typed into the code.

Common traps

  • Trap"Print a message and carry on when something fails."

    BetterExit with a non-zero code. Otherwise the scheduler thinks the run worked.

  • Trap"I will hardcode the API key to save time."

    BetterRead it from the environment. A key in the code ends up in every copy of the file.

  • Trap"It works on my laptop, so it is done."

    BetterRun it the way the team will: from the scheduler, with the real flags, and check the exit code.

Can you spot the problem?

Spot the problem

Which line, if someone added it to a claims export tool, would hide a failure from the scheduler?

Quick check

A nightly export must alert the support lead when it fails. Which design does that?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q399Parse a messy CSV/JSON file with edge cases.12 min
  2. Q397Take-home: a document agent with retrieval and tools.12 min
  3. Q398Take-home: messy data → records, with low-confidence flags.12 min
  4. Q401Re-engineering round: fix bugs in unfamiliar code.12 min
  5. Q402Learning round: pick up a new codebase or API fast.13 min

Last updated on 27 November 2026

Building a tool your team will run every night?

I design and build AI tools and agents, from the screens to the system behind them. Book a short call and tell me what the tool needs to do and who runs it.