---
title: "Unrolling the Codex agent loop"
description: "Codex CLI's agent loop repeatedly sends a prompt to the Responses API, executes any tool calls the model requests, and appends their outputs until the model returns an assistant message that ends the turn."
url: "https://swe.quest/articles/unrolling-the-codex-agent-loop-9f9b82453a544efc"
canonical: "https://openai.com/index/unrolling-the-codex-agent-loop"
source: "OpenAI"
published: "2026-01-23"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 16
tags: ["ai-agents","ai-coding","llms","developer-tools"]
---

# Unrolling the Codex agent loop

A post on [OpenAI](https://swe.quest/sources/openai.md), published 2026-01-23, 16 min read, Deep dive. Original: [https://openai.com/index/unrolling-the-codex-agent-loop](https://openai.com/index/unrolling-the-codex-agent-loop)

## Summary

Codex CLI's agent loop repeatedly sends a prompt to the Responses API, executes any tool calls the model requests, and appends their outputs until the model returns an assistant message that ends the turn. The initial prompt is assembled from three parts: instructions from config or bundled model files, a tools list (shell, update\_plan, web\_search, MCP tools), and ordered input items. Those input items cover sandbox permissions, developer instructions, aggregated AGENTS.md user instructions (32 KiB limit), skills and environment context. Responses stream back as SSE events, and reasoning and function-call items are fed back into later requests, so prompt length grows and context-window management becomes a core harness responsibility.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [AI agents](https://swe.quest/tags/ai-agents.md) (AI & ML)
- [AI for coding](https://swe.quest/tags/ai-coding.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Developer tools](https://swe.quest/tags/developer-tools.md) (Engineering practices)

## Prerequisites

- LLM tokens and context windows ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))
- HTTP APIs and JSON ([easy posts on API design](https://swe.quest/tags/api-design.md?difficulty=easy))
- tool calling in LLMs ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))

## Keywords

Codex CLI, Responses API, MCP, AGENTS.md, Server-Sent Events, gpt-oss

## Related posts

- [From model to agent: Equipping the Responses API with a computer environment](https://swe.quest/articles/from-model-to-agent-equipping-the-responses-api-with-a-computer-environment-102e043c05e5f9e2.md): OpenAI · 2026-03-11 · Medium · 10 min read · AI agents, LLMs, Sandboxing & isolation, System design
  OpenAI's Responses API now pairs with a shell tool and hosted container workspace so models can run multi-step agent workflows.
- [Four minutes after midnight, Codex said the page was live](https://swe.quest/articles/four-minutes-after-midnight-codex-said-the-page-was-live-25ae19da11733480.md): Render · 2026-08-16 · Medium · 9 min read · AI for coding, AI agents, Code review, Developer productivity
  OpenAI's Dominik Kundel argues that with agentic delegation, code is no longer the bottleneck.
- [You Should Write An Agent](https://swe.quest/articles/you-should-write-an-agent-1b4d0eb48fb350d8.md): Fly.io · 2025-11-06 · Medium · 10 min read · AI agents, LLMs, Python
  An LLM agent is just a loop around a stateless chat API: keep a list of messages, replay it on every call, and when the model emits a function\_call, run the tool and append its output.
- [Harness, Scaffold, and the AI Agent Terms Worth Getting Right](https://swe.quest/articles/harness-scaffold-and-the-ai-agent-terms-worth-getting-right-0e5aba5b0f70e57a.md): Hugging Face · 2026-05-25 · Easy · 12 min read · AI agents, LLMs, Model training
  A glossary of AI agent terms that are often conflated. The model is a stateless LLM.
- [Designing the hf CLI as an agent-optimized way to work with the Hub](https://swe.quest/articles/designing-the-hf-cli-as-an-agent-optimized-way-to-work-with-the-hub-e1ef73909553085b.md): Hugging Face · 2026-06-04 · Medium · 14 min read · Developer tools, AI agents, AI for coding, Evaluation
  Hugging Face rebuilt its hf CLI to serve both humans and coding agents.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
