Codex एजेंट लूप को समझना
(openai.com)- Codex CLI को लोकल environment में सुरक्षित और दक्ष तरीके से उच्च-गुणवत्ता वाले software changes करने वाले agent के रूप में डिज़ाइन किया गया है
- इसकी मुख्य संरचना agent loop है, जो user input, model inference और tool calls को चक्रीय रूप से जोड़कर सार्थक काम कराती है
- इस loop के दौरान बनने वाले prompt composition, context window management, और prompt caching performance और reliability के प्रमुख तत्व के रूप में काम करते हैं
- Codex, Responses API के जरिए model से संचार करता है, और हर request एक पूर्ण JSON payload से बनी होती है, जिससे stateless व्यवहार बना रहता है
- यह संरचना Zero Data Retention (ZDR), prompt caching, और automatic compaction जैसी उन्नत सुविधाओं को संभव बनाती है, और बड़े पैमाने के agent design की नींव तैयार करती है
Codex एजेंट लूप का अवलोकन
- Codex CLI, user, model, और tools के बीच interaction को orchestrate करने वाली loop structure के केंद्र में काम करता है
- user input लेकर model को भेजे जाने वाला prompt तैयार किया जाता है
- जब model response बनाता है या tool call का अनुरोध करता है, तो agent उसे चलाता है और उसका परिणाम फिर prompt में जोड़ देता है
- जब model आगे कोई tool call नहीं करता और assistant message बनाता है, तो एक turn समाप्त हो जाता है
- हर turn, conversation का हिस्सा होता है, और पिछली messages व tool call history अगली request के prompt में शामिल होती हैं
- क्योंकि prompt की लंबाई model की context window सीमा से प्रभावित होती है, Codex को इसका प्रबंधन करना पड़ता है
Responses API और Codex की संचार संरचना
- Codex CLI, model inference के लिए Responses API को HTTP requests भेजता है
- API endpoint configuration के अनुसार अलग हो सकता है, और OpenAI, ChatGPT, Azure, तथा local (LM Studio, Ollama आदि) environments में इस्तेमाल किया जा सकता है
- API request एक JSON payload से बनी होती है, और इसके मुख्य fields इस प्रकार हैं
- system/developer messages: model का मूल context सेट करना
- instructions: वे tools की सूची जिन्हें model call कर सकता है
- tools: Codex CLI, Responses API, और user (MCP server आदि) द्वारा दी गई tool definitions
- input: message list जिसमें conversation history और environment information शामिल होती है
- Codex,
~/.codex/config.tomlsettings और project के भीतरAGENTS.md,skillsfiles आदि को पढ़कर user instructions और environment information को अपने-आप insert करता है
Prompt composition और event handling
- Codex, हर message को JSON object (
type,role,content) के रूप में बनाकर Responses API को भेजता है - server, इसी JSON के आधार पर model prompt generate करता है और SSE (Server-Sent Events) stream के रूप में response लौटाता है
response.output_text.deltaevent streaming output के लिए उपयोग होता हैresponse.output_item.addedevent अगली request केinputमें जोड़ा जाता है, जिससे loop जारी रहता है
- इसे इस तरह डिज़ाइन किया गया है कि पिछला prompt, नए prompt का बिल्कुल सटीक prefix बने, ताकि prompt caching का लाभ लिया जा सके
Performance optimization: caching और stateless design
- Codex,
previous_response_idका उपयोग नहीं करता, इसलिए पूरी तरह stateless request structure बनाए रखता है- इससे Zero Data Retention (ZDR) customers के समर्थन और data retention को न्यूनतम रखने में मदद मिलती है
- prompt caching, समान prefix का पुन: उपयोग करके sampling cost को linear बनाती है
- cache hit केवल prompt के exact prefix match पर ही होता है
- tools list, model, sandbox settings, और working directory में बदलाव cache miss कराते हैं
- MCP tools में dynamic changes से cache loss हो सकता है, इसलिए Codex नई messages insert करने के तरीके से बदलावों को reflect करता है
Context window management और automatic compaction
- जब conversation लंबी हो जाती है, तो context window overflow से बचाने के लिए conversation compaction किया जाता है
- शुरुआत में
/compactcommand से manual summarization किया जाता था, लेकिन अब Responses API के/responses/compactendpoint का स्वत: उपयोग होता है - यह endpoint
type=compactionitem और encryptedencrypted_contentलौटाता है, ताकि model की समझ बनी रहे
- शुरुआत में
- Codex, auto_compact_limit पार होने पर अपने-आप compaction चलाता है, जिससे conversation continuity बनी रहती है
निष्कर्ष और आगे की दिशा
- Codex का agent loop, model inference, tool calls, caching, और context management को एकीकृत करने वाली मुख्य संरचना है
- यह संरचना high-performance, stateless, और security-focused agent design को संभव बनाती है
- आगे की posts में CLI architecture, tool usage implementation, और sandboxing model जैसी Codex की आंतरिक संरचना पर और चर्चा की जाएगी
1 टिप्पणियां
Hacker News की राय
इस ब्लॉग पोस्ट की सबसे अच्छी बात यह है कि इसमें कुछ भी चौंकाने वाला नहीं है। Codex CLI open source है, इसलिए reverse engineering के बिना अंदर क्या हो रहा है, यह देखा जा सकता है
Eric Traut (Pyright के लिए मशहूर developer) की communication भी बेहतरीन है। वे issues और PRs में सक्रिय रूप से भाग लेते हैं
GitHub repository
मैंने भी CLI में कुछ improvements contribute किए हैं, और releases व PRs को लगातार follow करके अपनी जानकारी बढ़ा रहा हूँ
दिलचस्प बात यह थी कि compaction को “मॉडल की latent understanding को सुरक्षित रखने वाले encrypted message” के रूप में किया जाता है
Codex, auto_compact_limit पार होने पर, इस endpoint का इस्तेमाल करके conversation context को efficiently छोटा कर देता है
Codex के अंदर झाँकते समय जो बात मुझे चौंकाने वाली लगी, वह यह थी कि reasoning tokens agent tool-calling loop में बने रहते हैं, लेकिन user turn बदलते ही हटा दिए जाते हैं
इसलिए कई turns तक context बनाए रखा जा सकता है, लेकिन आपस में संबंधित user requests के बीच कुछ context खो सकता है
मैं मॉडल से progress, plan, या debug details को markdown file में लिखवाता हूँ, ताकि वह कई context windows के बीच एक तरह के snapshot की तरह काम करे
GitHub repository
Codex में जो चीज़ मैं सच में चाहता हूँ, वह है Copilot-style checkpoints feature। GitHub पर इससे जुड़े कुछ issues हैं (#2788, #3585), लेकिन लगता है यह टीम की priority नहीं है
मैं जानना चाहता हूँ कि agent loop में user instructions को aggregate करते समय, multi-turn conversation में context retention को कैसे manage किया जाता है। यह भी जानना है कि user requirements बदलने पर dynamically adjust करने वाली techniques आज़माई गई हैं या नहीं
मुझे Codex पसंद है, लेकिन यह ChatGPT web interface से धीमा लगता है। जब ideas को जल्दी-जल्दी आगे-पीछे करना हो, तब अब भी web में copy-paste करना ज़्यादा productive लगता है
Codex अक्सर गलत code को edit करना शुरू कर देता है, इसलिए feedback loop धीमा और frustrating हो जाता है। फिर भी जब यह सही काम करता है, तो शानदार होता है। उम्मीद है कि कभी यह web जितना तेज़ और साथ ही local work के लायक भी हो जाएगा
यह खास नया नहीं था, लेकिन फिर भी काफ़ी उपयोगी लेख था। agentic coding CLI में loop या history पर आसानी से reflect कर पाना अच्छा होगा। मैंने MCP के ज़रिए chat history query करने का तरीका आज़माया है, लेकिन उसे explicitly specify करना पड़ता है, जो असुविधाजनक है। शायद continual learning इस समस्या को हल कर सके
इस तरह का behavior OTEL telemetry से भी देखा जा सकता है। मैं headless codex exec अक्सर इस्तेमाल करता हूँ, लेकिन built-in telemetry support कमजोर होने की वजह से debugging मुश्किल होती है
इसलिए मैं खुद codex-plus बनाकर इस्तेमाल कर रहा हूँ। यह codex exec interface को वैसे ही mirror करता है, TypeScript SDK के ऊपर implement किया गया है, और run के बाद session logs को remote OpenTelemetry collector में export करता है, ताकि codex-plus-log-viewer से उनका analysis किया जा सके
skills को समझाने वाला हिस्सा अजीब लगा
संबंधित code link
समझ नहीं आया कि files को सीधे expose करने के बजाय, मॉडल से उन्हें सामान्य files की तरह request क्यों करवाया गया
मैं जानना चाहता था कि क्या किसी ने Codex CLI को गंभीरता से इस्तेमाल किया है। मैंने VSCode Codex extension, Gemini CLI, और Claude Code CLI इस्तेमाल किए हैं, और तीनों की performance बहुत खराब थी।
लेकिन Rust में नया बनाया गया Codex CLI तो कमाल की performance देता है। UX भी बेहतरीन है, और shortcuts जैसी छोटी-छोटी चीज़ें भी अच्छे से की गई हैं। Theo ने कहा था कि “CLI optimization से ज़्यादा model improvements पर ध्यान देना चाहिए”, लेकिन इस्तेमाल करने के बाद मैं इससे बिल्कुल सहमत नहीं हूँ
संबंधित लेख: Scribe Swebench Benchmark