Building Command-Line Agent Interfaces
argparse, click, and Typer for agent CLI argument handling.
Why Build a CLI for Your Agent?
A command-line interface (CLI) makes your agent accessible from a terminal, scriptable in pipelines, and easy to test without a web UI. Many production agents are deployed as CLI tools.
Python has three excellent libraries for building CLIs: argparse (standard library), Typer, and Click.
argparse: The Standard Library Option
argparse is built into Python — no installation required. Use ArgumentParser() to define your interface, add_argument() to declare parameters, and parse_args() to process them.
import argparse
def main():
parser = argparse.ArgumentParser(
description='AI Agent CLI — ask questions and get answers'
)
parser.add_argument(
'--query',
type=str,
required=True,
help='The question to ask the agent'
)
parser.add_argument(
'--model',
type=str,
default='gpt-4o-mini',
help='OpenAI model to use (default: gpt-4o-mini)'
)
args = parser.parse_args()
print(f'Querying agent with: {args.query}')
print(f'Using model: {args.model}')
# result = run_agent(args.query, model=args.model)
if __name__ == '__main__':
main()All lessons in this course
- Building Command-Line Agent Interfaces
- Interactive REPL-Style Agents
- Argument Parsing and Help Text
- Streaming Output in CLI Agents