prompt_toolkit
Advanced interactive terminal UI library for Python — provides readline replacement with autocompletion, syntax highlighting, history, multi-line editing, and key bindings. prompt_toolkit features: prompt() for single-line input with completion, PromptSession for stateful sessions, completer=WordCompleter/NestedCompleter/FuzzyCompleter, history=FileHistory, lexer= for syntax highlighting (Pygments), vi_mode=True, key_bindings for custom shortcuts, HTML style formatting, multiline=True for multi-line editing, bottom_toolbar, rprompt, and async support (prompt_async()). Powers IPython, ptpython, and many CLIs.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Terminal input library. History files store all user input in plaintext — agent CLIs collecting sensitive input (passwords, API keys) must disable history for sensitive prompts: prompt('Password: ', is_password=True, history=InMemoryHistory()). is_password=True masks input with asterisks. History files should have restricted permissions (600).
⚡ Reliability
Best When
Building interactive Python CLIs, REPLs, or terminal applications that need autocomplete, syntax highlighting, and history — prompt_toolkit provides the complete readline replacement used by IPython and many modern Python CLIs.
Avoid When
You need simple input (use input()), GUI (use tkinter/PyQt), non-interactive scripting (use argparse), or are building server-side applications.
Use Cases
- • Agent interactive REPL — from prompt_toolkit import PromptSession; from prompt_toolkit.history import FileHistory; session = PromptSession(history=FileHistory('.agent_history')); while True: text = session.prompt('agent> '); execute_command(text) — REPL with command history; agent interactive CLI persists history across sessions; up/down arrows navigate history; Ctrl+R reverse search
- • Agent autocomplete CLI — from prompt_toolkit.completion import WordCompleter; completer = WordCompleter(['search', 'analyze', 'export', 'quit'], ignore_case=True); text = prompt('Command: ', completer=completer) — tab completion for commands; agent CLI shows available commands; FuzzyCompleter for fuzzy matching; NestedCompleter for sub-commands
- • Agent syntax-highlighted input — from prompt_toolkit.lexers import PygmentsLexer; from pygments.lexers import PythonLexer; code = prompt('>>> ', multiline=True, lexer=PygmentsLexer(PythonLexer)) — syntax-highlighted multi-line code input; agent code execution shell highlights Python syntax in input; multiline=True for multi-line paste
- • Agent dynamic completion — from prompt_toolkit.completion import Completer, Completion; class AgentCompleter(Completer): def get_completions(self, document, complete_event): word = document.get_word_before_cursor(); yield from (Completion(c, start_position=-len(word)) for c in get_matching_commands(word)) — dynamic completion from live data; agent tool completer queries registry for matching tool names
- • Agent async prompt — from prompt_toolkit import prompt_async; async def agent_loop(): while True: text = await prompt_async('> ', completer=completer) — async prompt in asyncio event loop; agent async application reads user input without blocking event loop; compatible with anyio and asyncio
Not For
- • Simple single-line input — prompt() is overkill for basic input(); for simple input use Python's built-in input()
- • GUI applications — prompt_toolkit is terminal-only; for GUI use tkinter, PyQt, or web UI
- • Server-side input — prompt_toolkit requires interactive TTY; for non-interactive scripting use argparse or click
Interface
Authentication
No auth — terminal UI library.
Pricing
prompt_toolkit is BSD licensed. Free for all use.
Agent Metadata
Known Gotchas
- ⚠ EOFError and KeyboardInterrupt require explicit handling — prompt() raises EOFError on Ctrl+D and KeyboardInterrupt on Ctrl+C; agent REPL without try/except exits on first Ctrl+C; proper REPL: try: text = session.prompt(); except KeyboardInterrupt: continue; except EOFError: break; omitting these handlers makes agent CLI fragile
- ⚠ Completion must be fast or async — get_completions() is called on every keystroke; slow completion (database queries, network calls) freezes input; agent dynamic completers must: 1) use ThreadedCompleter(SlowCompleter()) for thread pool, or 2) implement AsyncCompleter with async def get_completions(); synchronous slow completers degrade UX
- ⚠ FileHistory requires existing directory — FileHistory('~/.agent/history') raises FileNotFoundError if ~/.agent/ directory doesn't exist; agent CLI must create directory: os.makedirs('~/.agent', exist_ok=True) before FileHistory; InMemoryHistory for no persistence; use Path.expanduser() for ~ expansion in path
- ⚠ prompt() requires TTY — prompt() raises NotATerminalError when stdin is not a TTY (piped input, CI); agent CLI must check: import sys; if not sys.stdin.isatty(): use_non_interactive_mode(); prompt_toolkit cannot be used for non-interactive scripting; wrap prompt() calls for testing
- ⚠ vi_mode=True changes all key bindings — enabling vi mode changes navigation keys throughout input; Ctrl+C in insert mode vs normal mode behaves differently; agent vi-mode CLI must understand: Escape for normal mode, i for insert mode; keyboard shortcuts differ from default readline mode; document for users
- ⚠ HTML formatting in bottom_toolbar — bottom_toolbar='<b>Status:</b> OK' requires HTML formatter; plain string without HTML tags works but limiting; from prompt_toolkit.formatted_text import HTML: bottom_toolbar=HTML('<b>Status:</b> <style fg="green">OK</style>'); agent status bar with colors needs HTML wrapper; ANSI escape codes not interpreted in toolbar
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for prompt_toolkit.
Scores are editorial opinions as of 2026-03-06.