Agentic Code Generation with OpenAI Codex CLI — A Knight’s Tour Walkthrough

Hands-On with OpenAI Codex CLI

Agentic Code Generation with OpenAI Codex CLI

A step-by-step walkthrough of using OpenAI’s agentic coding tool to scaffold, solve, test, and visualise the classic Knight’s Tour chess problem — entirely through natural language prompts.

This post assumes you have Codex CLI installed and authenticated. We’ll be working in three phases, each driven by a carefully crafted Codex prompt.

♞ What is the Knight’s Tour Problem?

The Knight’s Tour is a classic puzzle from combinatorics and graph theory: given an n×n chessboard and a knight placed on any starting square, can the knight visit every square on the board exactly once using only valid knight moves? A knight moves in an L-shape — two squares in one direction and one square perpendicular, giving it up to eight possible moves from any position.

The problem has been studied for over a thousand years. Arab mathematicians documented it as early as the 9th century, and Leonhard Euler conducted a systematic mathematical analysis in 1759. There are two variants: an open tour, where the starting and ending squares differ, and a closed (re-entrant) tour, where the knight can return to its starting square in one move. On the standard 8×8 board, there are over 26 trillion distinct open tours.

From an algorithmic standpoint, the Knight’s Tour is a special case of the Hamiltonian path problem on a graph, where each square is a node and edges connect squares reachable by a knight move. Finding a Hamiltonian path is NP-complete in general, but the regular structure of the chessboard makes efficient heuristics possible.

The most well-known heuristic is Warnsdorff’s rule (H.C. von Warnsdorff, 1823): at each step, move to the unvisited square that has the fewest onward moves. This greedy approach runs in linear time relative to the number of squares and finds a tour almost always on boards of size 5×5 and above — which is exactly what we’ll ask Codex to implement.

🗺️ What We’re Building

By the end of this walkthrough, we’ll have a fully working Python application with three layers built up incrementally through Codex prompts:

PhaseWhat Codex BuildsOutput
Phase 1Core solver using Warnsdorff’s heuristicCLI app, ASCII board output
Phase 2Pytest test suite and colourised terminal outputANSI colour board, passing tests
Phase 3Flask web app with animated HTML canvasInteractive browser visualisation

⚙️ Prerequisites

Before starting, make sure you have:

  • Codex CLI installed (brew install --cask codex or npm i -g @openai/codex)
  • Authenticated with a ChatGPT Plus/Pro account or an OpenAI API key
  • Python 3.10+ available in your shell
  • Git installed on your machine (see Project Setup below if you haven’t configured it yet)

📁 Project Setup

We’ll use Git throughout this guide as a safety net — you can git diff to review what Codex changed, or revert entirely if a phase goes wrong. If this is your first time using Git on this machine, configure your identity first:

git config --global user.name  "Your Name"
git config --global user.email "[email protected]"

This only needs to be done once. To verify your settings at any time:

git config --global --list

Now create the project directory and initialise the repo:

mkdir knights-tour && cd knights-tour
git init
git commit --allow-empty -m "initial commit"

With the repo ready, launch Codex from inside the project directory:

codex

Once the TUI loads, your very first prompt should be to generate an AGENTS.md file. This acts as a standing project-level system prompt — Codex reads it automatically at the start of every future session, so you don’t have to repeat your conventions each time.

📝 Codex Prompt — Generate AGENTS.md

“Create an AGENTS.md file in the project root for a Python CLI and web application project. Include the following standing instructions: use Python 3.10+ with type hints throughout; place all tests in a /tests directory using pytest; never use global mutable state; prefer functions over classes unless OOP is genuinely the better fit; handle all CLI arguments with argparse; use Flask for any web routes; keep each module focused on a single responsibility. Format it as a markdown file with a brief intro line followed by a bullet list.”

Codex will create the file and show you the diff to review. Accept it, then commit before moving to Phase 1. Every subsequent Codex session in this directory will pick up these rules automatically.

💡 Why prompt Codex to write AGENTS.md instead of writing it yourself? Two reasons. First, you describe your intent conversationally rather than worrying about format. Second, it sets the right mental model for the rest of the guide — Codex writes the files, you review the diffs.

You can also update it at any time: “Add a rule that all functions must have docstrings” or “Update AGENTS.md to say we’re now using FastAPI instead of Flask” — Codex will edit the file in place and show you the diff.

Phase 1 — Core Solver

Still in the same Codex session, enter the Phase 1 prompt. Codex already has the project context from AGENTS.md, so you can get straight to the point:

📝 Codex Prompt — Phase 1

“Create a Python CLI app that solves the Knight’s Tour problem. Use Warnsdorff’s heuristic: at each step, move to the unvisited square with the fewest onward moves. The board size and starting position (row, col) should be configurable via argparse with defaults of n=8, row=0, col=0. The solver should return None if no tour is found. Display the completed board as a grid of move numbers, right-aligned. Save the solver logic in knight_tour.py and the entry point in main.py. Include a requirements.txt (even if empty for now) and a .gitignore for Python.”

Codex will show you its plan before making any changes. In Auto mode, you’ll see it create each file with a diff preview. Here’s a simplified version of what the generated knight_tour.py looks like:

MOVES = [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2)]

def get_neighbours(x, y, n, visited):
    return [(x+dx, y+dy) for dx,dy in MOVES
            if 0 <= x+dx < n and 0 <= y+dy < n
            and not visited[x+dx][y+dy]]

def solve(n, start_x, start_y):
    visited = [[False]*n for _ in range(n)]
    board   = [[-1]*n   for _ in range(n)]
    x, y = start_x, start_y
    visited[x][y] = True
    board[x][y]   = 0
    for move in range(1, n*n):
        neighbours = get_neighbours(x, y, n, visited)
        if not neighbours:
            return None
        x, y = min(neighbours,
                   key=lambda p: len(get_neighbours(p[0],p[1],n,visited)))
        visited[x][y] = True
        board[x][y]   = move
    return board

Run it in a separate terminal to verify:

python3 main.py --size 8 --row 0 --col 0

You should see a numbered 8×8 grid where each number represents the move order of the knight. Commit the result before moving to Phase 2.

💡 Approval Flow: In Codex’s default Auto mode, you’ll see a diff for each file before it’s written. Press A to accept or R to reject. If Codex proposes something you don’t want, reject it and follow up with a corrective prompt — it retains full context.

Phase 2 — Tests & Colourised Output

In the same Codex session, enter the next prompt. You don’t need to re-explain the project — Codex still has full context from Phase 1.

📝 Codex Prompt — Phase 2

“Now add two things. First, create a test suite in tests/test_knight_tour.py using pytest. Test that: (1) solve() returns a valid tour where every integer from 0 to n²−1 appears exactly once, (2) consecutive moves are a valid knight’s move apart, (3) solve() returns None for n=2 which has no solution. Second, add a new function print_coloured_board() in knight_tour.py that uses ANSI escape codes to colour the board — alternate between a light and dark background for a chess-style pattern, with white text for the move numbers. Call it from main.py instead of format_board() when –colour flag is passed.”

Codex will generate the test file and extend knight_tour.py with the colour function. Key tests:

import pytest
from knight_tour import solve

def is_valid_knight_move(x1, y1, x2, y2):
    dx, dy = abs(x2-x1), abs(y2-y1)
    return (dx, dy) in {(1,2),(2,1)}

@pytest.mark.parametrize("n,r,c", [(5,0,0),(6,1,1),(8,0,0),(8,3,4)])
def test_valid_tour(n, r, c):
    board = solve(n, r, c)
    assert board is not None
    flat = sorted(v for row in board for v in row)
    assert flat == list(range(n*n))

def test_no_solution_n2():
    assert solve(2, 0, 0) is None

Install pytest, then run the tests in a separate terminal:

pip3 install pytest
pytest tests/ -v

And try the colour flag:

python3 main.py --size 8 --colour

Phase 3 — Flask Web App with Animated Visualisation

Now we go beyond the terminal. In the same session (or a fresh one — Codex resumes from codex resume --last), enter the Phase 3 prompt:

📝 Codex Prompt — Phase 3

“Add a Flask web app in app.py. It needs two routes: GET / serves a single-page HTML form where users can input board size (5–10) and starting row/col. POST /solve accepts these inputs, runs the solver, and returns the board as JSON. The HTML page should also contain a JavaScript canvas visualisation that animates the knight’s path one move at a time when the solution arrives — draw the board as a grid, colour visited squares progressively, and draw a ♞ symbol on the current square. Add flask to requirements.txt.”

Codex generates app.py and embeds the full HTML/JS using Flask’s render_template_string. The key backend endpoint:

from flask import Flask, request, jsonify, render_template_string
from knight_tour import solve
app = Flask(__name__)

@app.route("/solve", methods=["POST"])
def solve_tour():
    data = request.get_json()
    n, row, col = int(data.get("size",8)), int(data.get("row",0)), int(data.get("col",0))
    if not (5 <= n <= 10):
        return jsonify({"error": "Board size must be between 5 and 10"}), 400
    board = solve(n, row, col)
    if board is None:
        return jsonify({"error": "No solution found"}), 422
    return jsonify({"board": board, "n": n})

Install Flask and run:

pip3 install flask
python3 app.py

Visit http://localhost:5000, choose your board size and starting square, hit Solve, and watch the knight’s path animate across the canvas.

🔄 Iterating Further — More Prompt Ideas

Once your three-phase app is working, you can keep iterating in the same session. Here are some prompts to take it further:

GoalFollow-up Prompt
Speed comparison“Add a backtracking solver as an alternative to Warnsdorff’s. Add a –solver flag to switch between them, and time both with Python’s timeit.”
Export result“Add a –export flag to main.py that saves the board as a CSV and also renders it as a PNG using matplotlib, with the knight path drawn as a line.”
User clicks board“Update the web app so users click a cell on the canvas to set the starting position instead of using the form fields.”
Code review“Review the current codebase for edge cases, type annotation completeness, and any issues with the input validation in app.py.”

✅ Effective Prompting Tips for Codex

A few patterns that made a noticeable difference in the quality of output across this walkthrough:

TipWhy It Helps
Name your files explicitlyCodex won’t guess at naming conventions. Saying “save to knight_tour.py” prevents it from choosing arbitrary filenames.
Specify what None/failure meansWithout “return None if no tour is found,” Codex might raise an exception instead — a valid choice but harder to test.
Bundle related changes in one promptPhase 2 added tests and colour output together. Codex handles multi-file tasks well when given in a single cohesive prompt.
Keep sessions alive for follow-upsDon’t exit and re-enter. Staying in the same session means Codex retains full context — you can make corrections without re-explaining the project.
Use AGENTS.md for standing rulesAnything you’d say in every prompt belongs in AGENTS.md. It keeps prompts shorter and ensures consistent style across sessions.

📁 Final Project Structure

knights-tour/
├── AGENTS.md
├── .gitignore
├── requirements.txt      ← flask
├── knight_tour.py        ← solver + display logic
├── main.py               ← CLI entry point
├── app.py                ← Flask web app
└── tests/
    └── test_knight_tour.py

What’s worth noticing in this walkthrough is the workflow rhythm: each Codex prompt builds on the last without re-explaining context, and the AGENTS.md file silently enforces project conventions so you don’t have to. The three phases took roughly 15 minutes end-to-end, most of which was reviewing diffs and running tests rather than writing code.

The Knight’s Tour is just the illustration. The same prompt-iterate-commit loop applies to any project — and the more you invest in a good AGENTS.md upfront, the more useful each Codex session becomes.

SingIDBot — Telegram ID Retrieval Bot for OpenClaw Topic Configuration

SETUP GUIDE
Build and deploy SingIDBot — a Telegram bot that retrieves Chat IDs and Topic Thread IDs, with a Singlish personality — on PythonAnywhere.

SingIDBot — Telegram ID Retrieval Bot for OpenClaw Topic Configuration

If you have been working through the OpenClaw Telegram integration guide, you already know that one of the trickiest steps is obtaining the correct Chat ID and Topic Thread ID before you can configure your bot endpoints. Getting those IDs wrong means your bot silently sends messages into the void — no errors, no feedback, just nothing arriving in the right place.

SingIDBot was built specifically to solve that problem. Drop it into any Telegram chat or group, fire the /getids command, and it will instantly report back the Chat ID and — crucially — the message_thread_id for whichever topic the command was sent from. No fiddling with Telegram’s Bot API directly, no JSON parsing, no guesswork.

Beyond the ID retrieval utility, SingIDBot has a second personality: it responds to free-text messages in Singlish — Singapore’s beloved colloquial English creole — cycling through randomised phrases like “Aiyoh, you finally came! Welcome lah!” and “Wah, interesting leh. Tell me more can?” This makes the bot noticeably more fun to test in group chats, and also serves as a practical demonstration of keyword-based message classification in python-telegram-bot v20+.

This guide walks through the complete source code, explains each architectural decision, and then covers deployment on PythonAnywhere including the free-tier network restrictions you will inevitably hit and exactly how to handle them.

Prerequisites
Requirement Details
Python 3.10+ Required for python-telegram-bot v20+ async support. PythonAnywhere provides Python 3.10 on all plans.
python-telegram-bot v20+ The async-native version of the library. Install with pip install python-telegram-bot. Version 20 introduced the Application.builder() pattern used throughout this bot.
Telegram Bot Token Create a new bot via @BotFather on Telegram. Send /newbot, follow the prompts, and copy the token. Keep it secret.
PythonAnywhere account A free Beginner account is sufficient to run and test the bot, but has an important network restriction covered in Section 3. A paid plan removes that restriction entirely.
Section 1 — What SingIDBot Does

The message_thread_id Mechanism

Telegram supergroups can be divided into named Topics, each identified by a message_thread_id. When a message is sent inside a topic, Telegram attaches this integer to the message object. When sent outside any topic, the field is absent entirely — not zero, not null, but genuinely missing.

This is the exact value OpenClaw requires to route notifications to the correct topic thread. SingIDBot’s /getids command reads message_thread_id from the incoming message and reports it in plain text for direct copy-paste into your configuration.

Because the field may be absent, the code uses getattr(update.effective_message, ‘message_thread_id’, None) rather than direct attribute access, which would raise an AttributeError in non-topic contexts.

The Singlish Responder

Any free-text message (not a command) triggers the singlish_chat handler. It classifies the message into greeting, thanks, or default using keyword matching, then picks a random phrase from the corresponding pool. A guard clause returns early if the message starts with / or was sent by the bot itself, preventing an infinite echo loop.

Section 2 — Code Walkthrough

2.1 Logging Setup

Two handlers run simultaneously: a FileHandler writing UTF-8 lines to singidbot.log, and a StreamHandler for the console. On PythonAnywhere you can watch live output in the bash session and audit the full log file afterward.

LOG_FILE = "singidbot.log"
logging.basicConfig(
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
    level=logging.INFO,
    handlers=[
        logging.FileHandler(LOG_FILE, encoding="utf-8"),
        logging.StreamHandler(),
    ],
)
logger = logging.getLogger(__name__)
Tip: The encoding=”utf-8″ argument is essential if any Telegram usernames contain non-ASCII characters. Without it, a single emoji in a username can crash the logger with a UnicodeEncodeError.

2.2 Singlish Response Bank

SINGLISH_RESPONSES maps category keys to phrase lists. get_singlish_response() uses dict.get() with a “default” fallback so unknown categories never raise a KeyError, and random.choice() picks uniformly from the pool.

SINGLISH_RESPONSES = {
    "greeting": [
        "Wah, you here ah! Come come, sit down lah!",
        "Eh hello! Long time no see, how are you?",
        "Aiyoh, you finally came! Welcome lah!",
        "Oi! Good to see you sia. What can I help you?",
    ],
    "thanks": [
        "Aiyah, no need to thank one lah!",
        "Wah, so polite! Welcome lah, anytime!",
        "No problem one! Next time also can help you.",
        "Eh, paiseh lah - that is what I am here for!",
    ],
    "default": [
        "Hah? Can repeat or not? I blur blur lah.",
        "Wah, interesting leh. Tell me more can?",
        "Aiyoh, I also dunno how to answer you sia.",
        "Got it lah! But I not so sure about that one.",
        "Eh, you very clever leh. I learn from you!",
    ],
}

def get_singlish_response(category):
    pool = SINGLISH_RESPONSES.get(category, SINGLISH_RESPONSES["default"])
    return random.choice(pool)

2.3 log_activity() Async Helper

log_activity() is declared async to match the handler context. It resolves the user’s display identifier to @username or numeric id, and emits a structured log line with a UTC ISO-8601 timestamp.

async def log_activity(user, event_type, metadata):
    user_info = (
        f"@{user.username}" if user and user.username
        else f"id={user.id}" if user
        else "unknown"
    )
    timestamp = datetime.now(timezone.utc).isoformat()
    logger.info(
        "[%s] event=%s user=%s metadata=%s",
        timestamp, event_type, user_info, metadata
    )

2.4 /start and /help Handlers

Both handlers log the event first, then compose and send a reply. /start personalises the greeting using user.first_name with a fallback to “friend”, prepending a random Singlish greeting to make every first interaction slightly different.

async def start(update, context):
    user = update.effective_user
    await log_activity(user, "START", {"chat_id": update.effective_chat.id})
    greeting = get_singlish_response("greeting")
    name = user.first_name if user and user.first_name else "friend"
    welcome_text = (
        f"{greeting}\n\nEh {name}, I am *SingIDBot* lah!\n\nCommands:\n- /start\n- /help\n- /getids"
    )
    await update.message.reply_text(welcome_text, parse_mode="Markdown")

async def help_command(update, context):
    user = update.effective_user
    await log_activity(user, "HELP", {"chat_id": update.effective_chat.id})
    await update.message.reply_text("*SingIDBot Commands* lah!\n/start /getids /help", parse_mode="Markdown")

2.5 /getids Handler

The key line is the getattr call. Telegram only populates message_thread_id when a message arrives inside a topic thread — in a regular chat the attribute simply does not exist. getattr with a None default handles this safely without raising AttributeError.

async def get_ids(update, context):
    user = update.effective_user
    await log_activity(user, "ID_RETRIEVAL", {"chat_id": update.effective_chat.id})
    chat_id = update.effective_chat.id
    topic_id = getattr(update.effective_message, "message_thread_id", None)
    response = f"Chat/Group ID: {chat_id}\n"
    if topic_id is not None:
        response += f"Topic ID: {topic_id}\n(This ID confirms the specific topic thread)"
    else:
        response += "Topic ID: Not available\n(Topics may not be enabled)"
    response += "\nTip: Record these IDs for configuring bot endpoints!"
    await update.message.reply_text(response, parse_mode="Markdown")
    await update.message.reply_text(f"{get_singlish_response('thanks')} Lah!")

2.6 singlish_chat Handler

Registered with filters.TEXT and ~filters.COMMAND. An internal guard also checks if the sender is the bot itself — without this, the bot’s own replies would trigger further replies in an infinite loop. Keyword classification checks thanks before greeting to avoid misclassification on mixed messages.

async def singlish_chat(update, context):
    user = update.effective_user
    await log_activity(user, "MESSAGE", {"text_preview": update.message.text[:50] if update.message else "N/A"})
    if (not update.message or update.message.text.startswith("/") or update.effective_user.id == context.bot.id):
        return
    user_text = update.message.text.lower().strip()
    if any(w in user_text for w in ["thanks", "thank", "appreciate"]):
        category = "thanks"
    elif any(w in user_text for w in ["hello", "hi", "hey", "oi", "wah", "alamak"]):
        category = "greeting"
    else:
        category = "default"
    await update.message.reply_text(get_singlish_response(category))

2.7 error_handler — Two-Tier Approach

Transient network issues (ProxyError, NetworkError, Conflict) are logged at WARNING level as a single line with no stack trace. Genuine bugs fall through to ERROR level with full exc_info traceback so they remain visible and actionable.

async def error_handler(update, context):
    error = context.error
    if isinstance(error, Exception):
        error_name = type(error).__name__
        error_msg = str(error)
        if "ProxyError" in error_msg or "NetworkError" in error_name:
            logger.warning("Network error (transient): %s", error_msg)
        elif "Conflict" in error_name or "terminated by other getUpdates" in error_msg:
            logger.warning("Conflict error (duplicate instance?): %s", error_msg)
        else:
            logger.error("Unhandled exception: %s: %s", error_name, error_msg, exc_info=context.error)

2.8 main() — Builder Chain, Timeouts, Handler Registration

All three timeout axes — connect, read, and write — are set to 30 seconds. Handlers are registered in priority order: specific commands first, then the catch-all text handler, then the error handler last.

def main():
    TOKEN = "YOUR_BOT_TOKEN_HERE"
    application = (
        Application.builder()
        .token(TOKEN)
        .connect_timeout(30)
        .read_timeout(30)
        .write_timeout(30)
        .build()
    )
    application.add_handler(CommandHandler("start", start))
    application.add_handler(CommandHandler("help", help_command))
    application.add_handler(CommandHandler("getids", get_ids))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, singlish_chat))
    application.add_error_handler(error_handler)
    logger.info("--- SingIDBot Initializing ---")
    application.run_polling(allowed_updates=Update.ALL_TYPES)
Section 3 — Deployment on PythonAnywhere

Upload singidbot.py via the Files tab, open a Bash console, and run:

pip install python-telegram-bot --upgrade
python singidbot.py

Free-Tier Proxy Restriction

Free accounts route outbound traffic through a proxy that does not whitelist api.telegram.org. Outbound sendMessage calls can fail with httpx.ProxyError: 503 Service Unavailable. Inbound polling via getUpdates works fine — it is only the reply direction that is intermittently blocked.

In practice across four live sessions totalling over 5 hours, only 2 ProxyErrors occurred in 1800+ polling cycles. The error handler suppresses these to a single WARNING line so the log stays clean.

Options if you need reliable reply delivery: Upgrade to a paid PythonAnywhere plan (removes proxy restrictions entirely), or switch from polling to webhook mode via PythonAnywhere’s WSGI layer (more complex but works on the free tier).

Avoiding Duplicate Instance Conflicts

Telegram only allows one active getUpdates poller per token. Always kill the old process before restarting:

pkill -f singidbot.py
pgrep -a -f singidbot
python singidbot.py
Section 4 — Log Analysis

The bot was tested across four live sessions on PythonAnywhere’s free tier.

Session Duration Key Events Errors
Session 1 ~2 min First run. Hello received; /getids succeeded returning Chat ID 5024469893. 1 ProxyError on reply (full traceback, no error handler yet).
Session 2 ~3 min Error handler added. /help ProxyError suppressed to single WARNING. /getids worked. Singlish replies all succeeded. 1 ProxyError (WARNING only).
Session 3 ~14 min Extended run. Second /help succeeded. 47 consecutive clean polling cycles. 1 ProxyError (WARNING only).
Session 4 ~5 hours 1800+ log lines. Clean polling throughout. No conflicts, no crashes. 2 ProxyErrors total across the entire session.

SingIDBot is production-stable on the free tier. The intermittent ProxyError is a platform constraint, not a code defect, and the error handler keeps it from polluting the log.

Quick Reference
Command What it returns Notes
/start Personalised Singlish greeting and command list. Works in any chat context.
/help List of all available commands. Works in any chat context.
/getids Chat ID and Topic Thread ID (if inside a topic). Must be run inside the target topic to get a valid Thread ID.

✦ This article was generated with the assistance of Claude by Anthropic