Interactive REPL-Style Agents
Input loops, history, and multi-turn conversation in terminal agents.
Interactive REPL-Style Agents is a free AI Agents lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a REPL-Style Agent?
A REPL (Read-Eval-Print Loop) is an interactive terminal session where you type input, get output, and continue. Chat agents in the terminal work exactly this way: the user types a message, the agent responds, and the conversation continues until the user exits.
The Basic REPL Loop
The simplest REPL is a while True loop that reads user input with input(), processes it, and prints the result. The loop runs forever until the user signals exit.
def run_agent_repl(inputs):
print('Agent started. Type /exit to quit.')
conversation_history = []
for user_input in inputs:
user_input = user_input.strip()
if not user_input:
continue
if user_input == '/exit':
print('Goodbye!')
break
conversation_history.append({'role': 'user', 'content': user_input})
agent_reply = f'[Agent reply to: {user_input}]'
conversation_history.append({'role': 'assistant', 'content': agent_reply})
print(f'Agent: {agent_reply}')
run_agent_repl(['Hello agent', 'What can you do?', '/exit'])Handling KeyboardInterrupt Gracefully
When a user presses Ctrl+C, Python raises a KeyboardInterrupt. Catch it at the top level to print a friendly goodbye message instead of a stack trace.
def run_agent_repl():
print('Agent ready. Press Ctrl+C to exit.')
conversation_history = []
try:
while True:
user_input = input('You: ').strip()
if not user_input:
continue
conversation_history.append({'role': 'user', 'content': user_input})
reply = '[mocked agent response]'
conversation_history.append({'role': 'assistant', 'content': reply})
print(f'Agent: {reply}')
except KeyboardInterrupt:
print('\nSession ended. Goodbye!')
except EOFError:
# Triggered when stdin is closed (piped input ends)
print('\nEnd of input. Exiting.')
if __name__ == '__main__':
import builtins
_demo_inputs = iter(['What agents can you build?', EOFError])
def _fake_input(prompt=''):
val = next(_demo_inputs)
if val is EOFError:
raise EOFError
print(prompt + val)
return val
builtins.input = _fake_input
run_agent_repl()
Maintaining Multi-Turn Conversation History
To give the agent memory of the conversation, maintain a list of messages and pass the full history to the LLM on each turn. Start with a system message that sets the agent's persona.
import openai
client = openai.OpenAI(api_key='YOUR_API_KEY')
def run_chat_repl():
history = [
{'role': 'system', 'content': 'You are a helpful AI assistant.'}
]
print('Chat started. Type /exit to quit.')
try:
while True:
user_input = input('You: ').strip()
if not user_input or user_input == '/exit':
break
history.append({'role': 'user', 'content': user_input})
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=history
)
reply = response.choices[0].message.content
history.append({'role': 'assistant', 'content': reply})
print(f'Agent: {reply}')
except KeyboardInterrupt:
print('\nGoodbye!')readline: Input History and Editing
The readline module (Unix/Mac) enables arrow-key navigation through previous inputs, just like a real shell. Users can press the up arrow to recall previous messages.
try:
import readline # available on Unix/Mac, not Windows
# Enable persistent history between sessions
import os
HISTORY_FILE = os.path.expanduser('~/.agent_history')
try:
readline.read_history_file(HISTORY_FILE)
except FileNotFoundError:
pass
import atexit
atexit.register(readline.write_history_file, HISTORY_FILE)
readline.set_history_length(500)
print('History enabled (use up/down arrows)')
except ImportError:
pass # readline not available on Windows
# After this, input() automatically has history supportBuilt-In Slash Commands
Give your REPL agent built-in slash commands for common operations: /help, /exit, /clear to reset conversation, /history to view past messages.
def handle_command(cmd: str, history: list) -> bool:
'Returns True if the command was handled, False if it is a regular message'
if cmd == '/exit' or cmd == '/quit':
print('Goodbye!')
raise SystemExit(0)
elif cmd == '/help':
print('Commands: /exit, /clear, /history, /help')
return True
elif cmd == '/clear':
# Keep only the system message
system = history[0] if history and history[0]['role'] == 'system' else None
history.clear()
if system:
history.append(system)
print('[Conversation cleared]')
return True
elif cmd == '/history':
for i, msg in enumerate(history):
if msg['role'] != 'system':
print(f' [{msg["role"]}] {msg["content"][:80]}')
return True
return False # not a command, treat as regular message
if __name__ == '__main__':
demo_history = [
{'role': 'system', 'content': 'You are a helpful agent.'},
{'role': 'user', 'content': 'Hi there'},
{'role': 'assistant', 'content': 'Hello! How can I help?'},
]
handle_command('/help', demo_history)
handle_command('/history', demo_history)
Limiting Conversation History Length
Passing the entire conversation history to the LLM on every turn grows the token count linearly. Trim the history to the last N messages (keeping the system message) to control costs.
def trim_history(history: list, max_turns: int = 10) -> list:
system_messages = [m for m in history if m['role'] == 'system']
non_system = [m for m in history if m['role'] != 'system']
# Keep the last max_turns messages (each turn = 1 user + 1 assistant)
max_messages = max_turns * 2
trimmed = non_system[-max_messages:]
return system_messages + trimmed
# Usage in the REPL loop:
# history.append({'role': 'user', 'content': user_input})
# trimmed = trim_history(history, max_turns=10)
# response = client.chat.completions.create(model='gpt-4o-mini', messages=trimmed)
print('Trimming history prevents token count from growing unboundedly')Displaying a Welcome Banner
A welcome banner makes your REPL feel polished. Print it once at startup with the agent's name, version, and available commands. Use simple ASCII art or the rich library for color.
def print_banner():
banner = '''
====================================
AI Research Agent v1.0
====================================
Type your question to begin.
Commands: /help /clear /exit
====================================
'''
print(banner)
# Or with rich colors:
# from rich.console import Console
# from rich.panel import Panel
# console = Console()
# console.print(Panel('[bold cyan]AI Research Agent[/] v1.0', subtitle='Type /help for commands'))
print_banner()Showing Typing Indicators
LLM calls can take several seconds. Show a 'thinking...' message while waiting so users know the agent is processing. Clear it when the response arrives.
import sys
import threading
import time
def thinking_spinner(stop_event: threading.Event):
frames = ['|', '/', '-', '\\']
i = 0
while not stop_event.is_set():
sys.stdout.write(f'\rAgent is thinking... {frames[i % 4]}')
sys.stdout.flush()
time.sleep(0.1)
i += 1
sys.stdout.write('\r' + ' ' * 30 + '\r') # clear the line
sys.stdout.flush()
def call_agent_with_spinner(query: str) -> str:
stop = threading.Event()
spinner = threading.Thread(target=thinking_spinner, args=(stop,))
spinner.start()
# result = agent.run(query) # the actual slow call
time.sleep(1) # simulating work
result = 'The answer is 42.'
stop.set()
spinner.join()
return result
if __name__ == '__main__':
answer = call_agent_with_spinner('What is 6 * 7?')
print(f'\nFinal answer: {answer}')
Saving Session to File
Allow users to save the conversation to a file at the end of a session. This is useful for research sessions where you want to keep a record of what the agent found.
import json
import datetime
import os
def save_session(history: list, directory: str = 'sessions'):
os.makedirs(directory, exist_ok=True)
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
filename = os.path.join(directory, f'session_{timestamp}.json')
with open(filename, 'w') as f:
json.dump({
'saved_at': timestamp,
'messages': history
}, f, indent=2)
print(f'Session saved to {filename}')
# Usage in REPL on /save command:
# save_session(conversation_history)
if __name__ == '__main__':
import tempfile
demo_dir = tempfile.mkdtemp()
demo_history = [
{'role': 'user', 'content': 'Hello'},
{'role': 'assistant', 'content': 'Hi! How can I help?'},
]
save_session(demo_history, directory=demo_dir)
Complete REPL Agent Template
Putting it all together: a production-ready REPL template with history management, slash commands, keyboard interrupt handling, and session saving.
def run_repl(agent):
history = [{'role': 'system', 'content': 'You are a helpful AI assistant.'}]
print('Agent ready. Type /help for commands.')
try:
while True:
user_input = input('You: ').strip()
if not user_input:
continue
if user_input.startswith('/'):
handle_command(user_input, history)
continue
history.append({'role': 'user', 'content': user_input})
trimmed = trim_history(history, max_turns=10)
reply = agent.run(trimmed)
history.append({'role': 'assistant', 'content': reply})
print(f'Agent: {reply}')
except KeyboardInterrupt:
print('\nSession ended.')
save_session(history)Knowledge Check: REPL-Style Agents
Test your understanding of interactive REPL agents.
Recap: Interactive REPL-Style Agents
You can now build a full-featured interactive agent for the terminal:
- Use
while True: input()as the core read loop - Catch
KeyboardInterruptfor clean Ctrl+C exit - Maintain a
conversation_historylist for multi-turn memory - Use
readlinefor arrow-key input history - Implement
/help,/clear,/exitslash commands - Trim history to prevent unbounded token growth
- Show a spinner or progress indicator during LLM calls
Frequently asked questions
Is the “Interactive REPL-Style Agents” lesson free?
Yes — the full text of “Interactive REPL-Style Agents” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Interactive REPL-Style Agents”?
Input loops, history, and multi-turn conversation in terminal agents. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Interactive REPL-Style Agents” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building Command-Line Agent Interfaces
- Interactive REPL-Style Agents
- Argument Parsing and Help Text
- Streaming Output in CLI Agents