Buckets:

download
raw
52.8 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;The Agentic Loop Deep Dive&quot;,&quot;local&quot;:&quot;the-agentic-loop-deep-dive&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Code Walkthrough&quot;,&quot;local&quot;:&quot;code-walkthrough&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Configuration&quot;,&quot;local&quot;:&quot;configuration&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;System Prompt&quot;,&quot;local&quot;:&quot;system-prompt&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Tool Definitions&quot;,&quot;local&quot;:&quot;tool-definitions&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;The Main Loop&quot;,&quot;local&quot;:&quot;the-main-loop&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Error Recovery&quot;,&quot;local&quot;:&quot;error-recovery&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Step Limit Safety&quot;,&quot;local&quot;:&quot;step-limit-safety&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Message History&quot;,&quot;local&quot;:&quot;message-history&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Context Management&quot;,&quot;local&quot;:&quot;context-management&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Design Decisions&quot;,&quot;local&quot;:&quot;design-decisions&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Key Takeaways&quot;,&quot;local&quot;:&quot;key-takeaways&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/context-course/pr_7/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/entry/start.55c3acaa.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/scheduler.4048030c.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/singletons.42509544.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/index.dd3f5310.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/paths.f6762227.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/entry/app.10ec5479.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/preload-helper.665302cf.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/index.5d0b9360.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/nodes/0.b63aa764.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/nodes/35.edbbb034.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.99c2e6c8.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/CodeBlock.ff2a3276.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;The Agentic Loop Deep Dive&quot;,&quot;local&quot;:&quot;the-agentic-loop-deep-dive&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Code Walkthrough&quot;,&quot;local&quot;:&quot;code-walkthrough&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Configuration&quot;,&quot;local&quot;:&quot;configuration&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;System Prompt&quot;,&quot;local&quot;:&quot;system-prompt&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Tool Definitions&quot;,&quot;local&quot;:&quot;tool-definitions&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;The Main Loop&quot;,&quot;local&quot;:&quot;the-main-loop&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Error Recovery&quot;,&quot;local&quot;:&quot;error-recovery&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Step Limit Safety&quot;,&quot;local&quot;:&quot;step-limit-safety&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Message History&quot;,&quot;local&quot;:&quot;message-history&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Context Management&quot;,&quot;local&quot;:&quot;context-management&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Design Decisions&quot;,&quot;local&quot;:&quot;design-decisions&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Key Takeaways&quot;,&quot;local&quot;:&quot;key-takeaways&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="the-agentic-loop-deep-dive" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-agentic-loop-deep-dive"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The Agentic Loop Deep Dive</span></h1> <p data-svelte-h="svelte-1q28z8l">The core of nano_harness is a loop that runs at most fifty times: call the LLM, parse its code, execute it, observe results, repeat.</p> <iframe src="https://context-course-agent-loop.static.hf.space" frameborder="0" width="850" height="450"></iframe> <h2 class="relative group"><a id="code-walkthrough" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#code-walkthrough"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Code Walkthrough</span></h2> <h3 class="relative group"><a id="configuration" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#configuration"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Configuration</span></h3> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->TASK = <span class="hljs-string">&quot;Inspect the workspace and provide a summary.&quot;</span>
MODEL = os.getenv(<span class="hljs-string">&quot;NANO_MODEL&quot;</span>, <span class="hljs-string">&quot;zai-org/GLM-5.1&quot;</span>)
BASE_URL = os.getenv(<span class="hljs-string">&quot;OPENAI_BASE_URL&quot;</span>, <span class="hljs-string">&quot;https://router.huggingface.co/v1&quot;</span>)
API_KEY = os.getenv(<span class="hljs-string">&quot;HF_TOKEN&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
WORKSPACE = <span class="hljs-built_in">str</span>(Path.cwd())
MAX_STEPS = <span class="hljs-number">50</span>
TEMPERATURE = <span class="hljs-number">0.2</span>
TIMEOUT_S = <span class="hljs-number">30</span>
MAX_CHARS = <span class="hljs-number">8000</span>
ALLOW_WRITE = <span class="hljs-literal">False</span>
ALLOW_COMMANDS = [<span class="hljs-string">&quot;ls&quot;</span>, <span class="hljs-string">&quot;cat&quot;</span>, <span class="hljs-string">&quot;pwd&quot;</span>, <span class="hljs-string">&quot;echo&quot;</span>, <span class="hljs-string">&quot;head&quot;</span>, <span class="hljs-string">&quot;tail&quot;</span>, <span class="hljs-string">&quot;wc&quot;</span>, <span class="hljs-string">&quot;rg&quot;</span>]<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1ij4ah8"><code>MODEL</code> is the HF model ID routed through Inference Providers, <code>MAX_STEPS</code> caps iterations at 50, <code>TEMPERATURE=0.2</code> keeps output deterministic, <code>ALLOW_COMMANDS</code> is the shell allowlist, <code>ALLOW_WRITE=False</code> keeps file mutation off by default, and <code>MAX_CHARS</code> bounds tool output to prevent context overflow.</p> <h3 class="relative group"><a id="system-prompt" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#system-prompt"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>System Prompt</span></h3> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->SYSTEM_PROMPT = <span class="hljs-string">f&quot;&quot;&quot;You are a code-first agent.
Output only executable Python code, no prose.
Tools available:
- list_dir(path=&#x27;.&#x27;): List directory contents
- read_file(path, max_chars=4000): Read file
- write_file(path, content): Write file (only if ALLOW_WRITE=True)
- exec_cmd(args): Run shell command
When task is complete, call:
final_answer(result)
Constraints:
- All file paths confined to workspace: <span class="hljs-subst">{WORKSPACE}</span>
- Allowed commands: <span class="hljs-subst">{ALLOW_COMMANDS}</span>
- Max output: <span class="hljs-subst">{MAX_CHARS}</span> chars
- No markdown, no prose—only Python
&quot;&quot;&quot;</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-xjn4n1">The system prompt tells the model to output only Python code, lists the available tools, shows how to signal completion with <code>final_answer()</code>, and states the path, command, and size constraints.</p> <h3 class="relative group"><a id="tool-definitions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tool-definitions"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tool Definitions</span></h3> <p data-svelte-h="svelte-15ge6x">The tools are defined as Python functions that the model can call:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">list_dir</span>(<span class="hljs-params">path=<span class="hljs-string">&quot;.&quot;</span></span>):
<span class="hljs-string">&quot;&quot;&quot;List directory contents.&quot;&quot;&quot;</span>
p = safe_path(path) <span class="hljs-comment"># Ensure path is in workspace</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> p.is_dir():
<span class="hljs-keyword">raise</span> NotADirectoryError(<span class="hljs-built_in">str</span>(p))
<span class="hljs-keyword">return</span> <span class="hljs-built_in">sorted</span>([x.name + (<span class="hljs-string">&quot;/&quot;</span> <span class="hljs-keyword">if</span> x.is_dir() <span class="hljs-keyword">else</span> <span class="hljs-string">&quot;&quot;</span>) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> p.iterdir()])
<span class="hljs-keyword">def</span> <span class="hljs-title function_">read_file</span>(<span class="hljs-params">path, max_chars=<span class="hljs-number">4000</span></span>):
<span class="hljs-string">&quot;&quot;&quot;Read file with size limit.&quot;&quot;&quot;</span>
p = safe_path(path)
content = p.read_text(encoding=<span class="hljs-string">&quot;utf-8&quot;</span>, errors=<span class="hljs-string">&quot;replace&quot;</span>)
<span class="hljs-keyword">return</span> clip(content, <span class="hljs-built_in">min</span>(max_chars, MAX_CHARS)) <span class="hljs-comment"># Limit output</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">write_file</span>(<span class="hljs-params">path, content</span>):
<span class="hljs-string">&quot;&quot;&quot;Write or create file if writes are enabled.&quot;&quot;&quot;</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> ALLOW_WRITE:
<span class="hljs-keyword">raise</span> PermissionError(<span class="hljs-string">&quot;write_file disabled&quot;</span>)
p = safe_path(path)
p.parent.mkdir(parents=<span class="hljs-literal">True</span>, exist_ok=<span class="hljs-literal">True</span>)
p.write_text(<span class="hljs-built_in">str</span>(content), encoding=<span class="hljs-string">&quot;utf-8&quot;</span>)
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Wrote <span class="hljs-subst">{<span class="hljs-built_in">len</span>(<span class="hljs-built_in">str</span>(content))}</span> bytes&quot;</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">exec_cmd</span>(<span class="hljs-params">args</span>):
<span class="hljs-string">&quot;&quot;&quot;Execute shell command (whitelisted only).&quot;&quot;&quot;</span>
<span class="hljs-keyword">if</span> args[<span class="hljs-number">0</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> ALLOW_COMMANDS:
<span class="hljs-keyword">raise</span> PermissionError(<span class="hljs-string">f&quot;Command <span class="hljs-subst">{args[<span class="hljs-number">0</span>]}</span> not allowed&quot;</span>)
result = subprocess.run(args, capture_output=<span class="hljs-literal">True</span>, timeout=TIMEOUT_S, text=<span class="hljs-literal">True</span>)
output_parts = []
<span class="hljs-keyword">if</span> result.stdout:
output_parts.append(<span class="hljs-string">f&quot;stdout:\n<span class="hljs-subst">{result.stdout}</span>&quot;</span>)
<span class="hljs-keyword">if</span> result.stderr:
output_parts.append(<span class="hljs-string">f&quot;stderr:\n<span class="hljs-subst">{result.stderr}</span>&quot;</span>)
output = <span class="hljs-string">&quot;\n\n&quot;</span>.join(output_parts) <span class="hljs-keyword">or</span> <span class="hljs-string">f&quot;(exit code <span class="hljs-subst">{result.returncode}</span> with no output)&quot;</span>
<span class="hljs-keyword">return</span> clip(output, MAX_CHARS)
DONE = <span class="hljs-literal">False</span>
FINAL_RESULT = <span class="hljs-literal">None</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">final_answer</span>(<span class="hljs-params">value</span>):
<span class="hljs-string">&quot;&quot;&quot;Agent calls this when task is complete.&quot;&quot;&quot;</span>
<span class="hljs-keyword">global</span> DONE, FINAL_RESULT
DONE = <span class="hljs-literal">True</span>
FINAL_RESULT = value
<span class="hljs-keyword">return</span> value<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-yo0alr">Each tool enforces safety at the boundary: path confinement, command allowlisting, output size limits, and explicit write gating.</p> <h3 class="relative group"><a id="the-main-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-main-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The Main Loop</span></h3> <p data-svelte-h="svelte-zkuwo1">The harness uses the Responses API. The OpenAI SDK accepts message-style <code>input</code> payloads and returns <code>response.output_text</code>, and the Hugging Face router exposes the same <code>/v1</code> surface via Inference Providers, so a single model ID (<code>zai-org/GLM-5.1</code>) drives the whole unit.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>():
<span class="hljs-keyword">global</span> DONE, FINAL_RESULT
DONE = <span class="hljs-literal">False</span>
FINAL_RESULT = <span class="hljs-literal">None</span>
messages = [
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: SYSTEM_PROMPT},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: TASK}
]
<span class="hljs-keyword">for</span> step <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(MAX_STEPS):
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\n[Step <span class="hljs-subst">{step + <span class="hljs-number">1</span>}</span>]&quot;</span>)
<span class="hljs-comment"># 1. Call LLM</span>
response = client.responses.create(
model=MODEL,
temperature=TEMPERATURE,
<span class="hljs-built_in">input</span>=messages
)
content = response.output_text
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model output:\n<span class="hljs-subst">{content[:<span class="hljs-number">500</span>]}</span>...&quot;</span>)
<span class="hljs-comment"># 2. Add model response to history</span>
messages.append({<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: content})
<span class="hljs-comment"># 3. Parse and execute Python code</span>
code = extract_python(content) <span class="hljs-comment"># Parse code block from response</span>
<span class="hljs-keyword">try</span>:
stdout_buffer = io.StringIO()
stderr_buffer = io.StringIO()
exec_globals = {
<span class="hljs-string">&quot;__builtins__&quot;</span>: {},
<span class="hljs-string">&quot;list_dir&quot;</span>: list_dir,
<span class="hljs-string">&quot;read_file&quot;</span>: read_file,
<span class="hljs-string">&quot;write_file&quot;</span>: write_file,
<span class="hljs-string">&quot;exec_cmd&quot;</span>: exec_cmd,
<span class="hljs-string">&quot;final_answer&quot;</span>: final_answer
}
<span class="hljs-keyword">with</span> redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
<span class="hljs-built_in">exec</span>(code, exec_globals)
stdout_text = stdout_buffer.getvalue().strip()
stderr_text = stderr_buffer.getvalue().strip()
<span class="hljs-keyword">if</span> DONE:
result = <span class="hljs-string">f&quot;Final answer: <span class="hljs-subst">{clip(FINAL_RESULT)}</span>&quot;</span>
<span class="hljs-keyword">else</span>:
observations = []
<span class="hljs-keyword">if</span> stdout_text:
observations.append(<span class="hljs-string">f&quot;stdout:\n<span class="hljs-subst">{clip(stdout_text)}</span>&quot;</span>)
<span class="hljs-keyword">if</span> stderr_text:
observations.append(<span class="hljs-string">f&quot;stderr:\n<span class="hljs-subst">{clip(stderr_text)}</span>&quot;</span>)
result = <span class="hljs-string">&quot;\n\n&quot;</span>.join(observations) <span class="hljs-keyword">or</span> <span class="hljs-string">&quot;Executed successfully (no output)&quot;</span>
<span class="hljs-keyword">except</span> FileNotFoundError:
result = <span class="hljs-string">&quot;Error: FileNotFoundError: File not found&quot;</span>
<span class="hljs-keyword">except</span> PermissionError <span class="hljs-keyword">as</span> e:
result = <span class="hljs-string">f&quot;Error: PermissionError: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-keyword">except</span> subprocess.TimeoutExpired:
result = <span class="hljs-string">&quot;Error: TimeoutError: Command took too long&quot;</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
result = <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">type</span>(e).__name__}</span>: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-comment"># 4. Check if agent called final_answer()</span>
<span class="hljs-keyword">if</span> DONE:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;✓ Task complete: <span class="hljs-subst">{FINAL_RESULT}</span>&quot;</span>)
<span class="hljs-keyword">break</span>
<span class="hljs-comment"># 5. Add observation to message history</span>
messages.append({<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: result})
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> DONE:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;✗ Max steps (<span class="hljs-subst">{MAX_STEPS}</span>) reached without final_answer()&quot;</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1g0zcc8">Four things matter in this loop. The message history accumulates system prompt, user task, and alternating assistant/observation turns. The LLM call uses a configurable model and a low temperature (0.2) for deterministic behavior. Code execution runs through <code>exec()</code> with a restricted globals dict that strips builtins and exposes only the tool functions. The loop then feeds back one of four observation types: stdout, stderr, an explicit final answer, or a structured error string.</p> <h2 class="relative group"><a id="error-recovery" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#error-recovery"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Error Recovery</span></h2> <p data-svelte-h="svelte-vgzexn">The main loop above already handles the common failure modes explicitly:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">try</span>:
<span class="hljs-keyword">with</span> redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
<span class="hljs-built_in">exec</span>(code, exec_globals)
<span class="hljs-keyword">except</span> FileNotFoundError:
result = <span class="hljs-string">&quot;Error: FileNotFoundError: File not found&quot;</span>
<span class="hljs-keyword">except</span> PermissionError <span class="hljs-keyword">as</span> e:
result = <span class="hljs-string">f&quot;Error: PermissionError: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-keyword">except</span> subprocess.TimeoutExpired:
result = <span class="hljs-string">&quot;Error: TimeoutError: Command took too long&quot;</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
result = <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">type</span>(e).__name__}</span>: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-comment"># The model sees this observation and tries a different approach</span>
messages.append({<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: result})<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-15oaevw">The agent reads the failure as an observation and adjusts: list the directory after a missing file, stop trying <code>write_file</code> when writes are disabled, swap a blocked command for a whitelisted one, or break long-running work into smaller steps.</p> <h2 class="relative group"><a id="step-limit-safety" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#step-limit-safety"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Step Limit Safety</span></h2> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">for</span> step <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(MAX_STEPS): <span class="hljs-comment"># MAX_STEPS = 50</span>
<span class="hljs-comment"># ... run loop ...</span>
<span class="hljs-keyword">if</span> DONE:
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> DONE:
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Max steps reached without final_answer()&quot;</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-bqibq8">The step limit guarantees termination. Simple tasks like “list files” finish in one to three steps; exploring a codebase typically takes five to ten; debugging something non-trivial can run fifteen to thirty.</p> <h2 class="relative group"><a id="message-history" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#message-history"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Message History</span></h2> <p data-svelte-h="svelte-1pw2qfv">The message history is the agent’s memory:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->messages = [
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;You are a code-first agent...&quot;</span>},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;Inspect workspace and summarize&quot;</span>},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;list_dir(&#x27;.&#x27;)\nread_file(&#x27;README.md&#x27;)&quot;</span>},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;Found: [&#x27;README.md&#x27;, &#x27;src/&#x27;, &#x27;tests/&#x27;]\n\nREADME.md contains:\n...&quot;</span>},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;read_file(&#x27;src/main.py&#x27;)&quot;</span>},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;src/main.py:\n...&quot;</span>},
]<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-p3ukmq">Every entry is a turn: system prompt, user task, then alternating agent code and observations. The LLM sees the whole history on each call, so it can refer back to earlier findings, avoid approaches that already failed, and build on what it learned.</p> <blockquote class="note" data-svelte-h="svelte-19tdqvm"><p><strong>This is a simplification.</strong> The nano harness treats memory as a flat conversation history — every prior turn stays in context until the window fills up. Production agent systems use much richer memory architectures: short-term scratchpads for working state, episodic memory for recalling past sessions, semantic memory for persistent knowledge, retrieval-augmented approaches that fetch relevant memories on demand, and compaction strategies that summarize older context to free up space. If you want to go deeper, look into the research on agent memory systems (e.g., MemoryAgentBench, A-MEM) and context compression (e.g., ACON). The nano harness is a teaching tool — it shows the minimal viable loop, not the full picture.</p></blockquote> <h2 class="relative group"><a id="context-management" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#context-management"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Context Management</span></h2> <p data-svelte-h="svelte-1on936j">With MAX_TOKENS=4096 and MAX_CHARS=8000:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># Good: Agent reads one file at a time</span>
read_file(<span class="hljs-string">&quot;test.py&quot;</span>, max_chars=<span class="hljs-number">2000</span>) <span class="hljs-comment"># 2000 chars ✓</span>
<span class="hljs-comment"># Bad: Agent tries to read entire codebase at once</span>
read_file(<span class="hljs-string">&quot;large_codebase.py&quot;</span>, max_chars=<span class="hljs-number">50000</span>) <span class="hljs-comment"># Gets clipped to 8000</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-12sf4q5">The agent learns to read strategically to stay within limits.</p> <blockquote class="note" data-svelte-h="svelte-1quivc4"><p><strong>Context management in production is a major topic.</strong> Real code agents implement compaction (summarizing earlier context), structured note-taking (maintaining scratchpads of key findings), file-system-mediated context (writing intermediate results to files instead of keeping them in the window), and intelligent tool selection to minimize context consumption. Anthropic’s context engineering guide describes these as core concerns for any serious agent deployment. The nano harness only demonstrates the simplest approach: hard character limits and hoping the agent reads strategically.</p></blockquote> <h2 class="relative group"><a id="design-decisions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#design-decisions"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Design Decisions</span></h2> <p data-svelte-h="svelte-890vlv">Python is precise where JSON or free-form text is ambiguous, so the agent outputs code. <code>safe_path()</code> resolves and validates every path against the workspace root to prevent directory traversal. Only an explicit allowlist of shell commands can run. A hard step limit and per-call output limit bound both runtime and context growth. And because exceptions are turned back into observations, the agent adapts instead of crashing.</p> <h2 class="relative group"><a id="key-takeaways" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#key-takeaways"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Key Takeaways</span></h2> <p data-svelte-h="svelte-17roieu">Call the LLM, parse its code, execute it with tools, observe, repeat. The system prompt defines tools, constraints, and the termination signal. Sandboxing happens at the tool boundary through path confinement, command allowlists, and size limits. Errors become observations and the full message history serves as memory.</p> <p data-svelte-h="svelte-1dlyagd">Next, tools and sandboxing in more detail.</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/context-course/blob/main/units/en/unit6/agent-loop.mdx" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_ck2jc = {
assets: "/docs/context-course/pr_7/en",
base: "/docs/context-course/pr_7/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/context-course/pr_7/en/_app/immutable/entry/start.55c3acaa.js"),
import("/docs/context-course/pr_7/en/_app/immutable/entry/app.10ec5479.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 35],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
52.8 kB
·
Xet hash:
b1fba51a5ad7b9514fb7ce7f8e2364c806b88ddee68f0977496d5ee7cf93be09

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.