Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Hands-On: Build an Agent Activity Dashboard with Gradio","local":"hands-on-build-an-agent-activity-dashboard-with-gradio","sections":[{"title":"What You’ll Build","local":"what-youll-build","sections":[],"depth":2},{"title":"Project Setup","local":"project-setup","sections":[],"depth":2},{"title":"Step 1: Build the Receiver and Dashboard","local":"step-1-build-the-receiver-and-dashboard","sections":[],"depth":2},{"title":"Step 2: Connect Your Agent","local":"step-2-connect-your-agent","sections":[],"depth":2},{"title":"Step 3: Watch It Live","local":"step-3-watch-it-live","sections":[],"depth":2},{"title":"Step 4: Add a Guardrail","local":"step-4-add-a-guardrail","sections":[],"depth":2},{"title":"Step 5: Deploy (Optional)","local":"step-5-deploy-optional","sections":[],"depth":2},{"title":"Full Directory Layout","local":"full-directory-layout","sections":[],"depth":2},{"title":"Best Practices Demonstrated","local":"best-practices-demonstrated","sections":[],"depth":2},{"title":"Key Takeaways","local":"key-takeaways","sections":[],"depth":2}],"depth":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/30.02bc8e8c.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"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/HfOption.0fd5270b.js"> | |
| <link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/stores.0a12db33.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Hands-On: Build an Agent Activity Dashboard with Gradio","local":"hands-on-build-an-agent-activity-dashboard-with-gradio","sections":[{"title":"What You’ll Build","local":"what-youll-build","sections":[],"depth":2},{"title":"Project Setup","local":"project-setup","sections":[],"depth":2},{"title":"Step 1: Build the Receiver and Dashboard","local":"step-1-build-the-receiver-and-dashboard","sections":[],"depth":2},{"title":"Step 2: Connect Your Agent","local":"step-2-connect-your-agent","sections":[],"depth":2},{"title":"Step 3: Watch It Live","local":"step-3-watch-it-live","sections":[],"depth":2},{"title":"Step 4: Add a Guardrail","local":"step-4-add-a-guardrail","sections":[],"depth":2},{"title":"Step 5: Deploy (Optional)","local":"step-5-deploy-optional","sections":[],"depth":2},{"title":"Full Directory Layout","local":"full-directory-layout","sections":[],"depth":2},{"title":"Best Practices Demonstrated","local":"best-practices-demonstrated","sections":[],"depth":2},{"title":"Key Takeaways","local":"key-takeaways","sections":[],"depth":2}],"depth":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="hands-on-build-an-agent-activity-dashboard-with-gradio" 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="#hands-on-build-an-agent-activity-dashboard-with-gradio"><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>Hands-On: Build an Agent Activity Dashboard with Gradio</span></h1> <p data-svelte-h="svelte-1v0okw4">This project wires the hooks you configured in the previous lesson into a live dashboard. The dashboard is a Gradio app that accepts hook events over HTTP, shows the most recent calls in a table, and plots tool usage over time. It works against Claude Code, Codex, OpenCode, and Pi — with the same agent doing the work, you see what actually happens under the hood.</p> <h2 class="relative group"><a id="what-youll-build" 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="#what-youll-build"><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>What You’ll Build</span></h2> <p data-svelte-h="svelte-bv11ys">One Python process that does two things at once:</p> <ol data-svelte-h="svelte-6jlqsf"><li>A <strong>FastAPI</strong> endpoint at <code>POST /event</code> that any hook can post to.</li> <li>A <strong>Gradio</strong> app mounted on the same server that renders the events live.</li></ol> <p data-svelte-h="svelte-18lrl7m">The Gradio app polls the in-memory event buffer and re-renders every second, so you can watch the agent work in real time.</p> <h2 class="relative group"><a id="project-setup" 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="#project-setup"><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>Project Setup</span></h2> <p data-svelte-h="svelte-k8ffup">Create a fresh project directory:</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-built_in">mkdir</span> agent-activity-dashboard | |
| <span class="hljs-built_in">cd</span> agent-activity-dashboard<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-axv5aq">Install dependencies:</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 -->pip install <span class="hljs-string">"gradio>=4.41"</span> <span class="hljs-string">"fastapi"</span> <span class="hljs-string">"uvicorn[standard]"</span> <span class="hljs-string">"pandas"</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1wif1w7">Create <code>requirements.txt</code> for later deployment:</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 -->gradio>=4.41 | |
| fastapi | |
| uvicorn[standard] | |
| pandas<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="step-1-build-the-receiver-and-dashboard" 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-1-build-the-receiver-and-dashboard"><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 1: Build the Receiver and Dashboard</span></h2> <p data-svelte-h="svelte-v6usc5">Create <code>app.py</code>:</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">import</span> datetime <span class="hljs-keyword">as</span> dt | |
| <span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Counter, deque | |
| <span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">Any</span> | |
| <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr | |
| <span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd | |
| <span class="hljs-keyword">from</span> fastapi <span class="hljs-keyword">import</span> FastAPI, Request | |
| <span class="hljs-keyword">from</span> fastapi.responses <span class="hljs-keyword">import</span> JSONResponse | |
| <span class="hljs-comment"># ---------- Shared state ----------</span> | |
| MAX_EVENTS = <span class="hljs-number">500</span> | |
| events: deque[<span class="hljs-built_in">dict</span>[<span class="hljs-built_in">str</span>, <span class="hljs-type">Any</span>]] = deque(maxlen=MAX_EVENTS) | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_truncate</span>(<span class="hljs-params">value: <span class="hljs-type">Any</span>, n: <span class="hljs-built_in">int</span></span>) -> <span class="hljs-built_in">str</span>: | |
| text = <span class="hljs-string">""</span> <span class="hljs-keyword">if</span> value <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">else</span> <span class="hljs-built_in">str</span>(value) | |
| <span class="hljs-keyword">return</span> text <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(text) <= n <span class="hljs-keyword">else</span> text[: n - <span class="hljs-number">1</span>] + <span class="hljs-string">"…"</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_normalize</span>(<span class="hljs-params">body: <span class="hljs-built_in">dict</span>[<span class="hljs-built_in">str</span>, <span class="hljs-type">Any</span>], headers: <span class="hljs-built_in">dict</span>[<span class="hljs-built_in">str</span>, <span class="hljs-built_in">str</span>]</span>) -> <span class="hljs-built_in">dict</span>[<span class="hljs-built_in">str</span>, <span class="hljs-type">Any</span>]: | |
| <span class="hljs-string">"""Map Claude Code / Codex / OpenCode / Pi payloads to one shape."""</span> | |
| platform = ( | |
| body.get(<span class="hljs-string">"platform"</span>) | |
| <span class="hljs-keyword">or</span> headers.get(<span class="hljs-string">"x-platform"</span>) | |
| <span class="hljs-keyword">or</span> <span class="hljs-string">"unknown"</span> | |
| ) | |
| event_name = body.get(<span class="hljs-string">"event"</span>) <span class="hljs-keyword">or</span> body.get(<span class="hljs-string">"hook_event_name"</span>) <span class="hljs-keyword">or</span> <span class="hljs-string">"Unknown"</span> | |
| tool = body.get(<span class="hljs-string">"tool"</span>) <span class="hljs-keyword">or</span> body.get(<span class="hljs-string">"tool_name"</span>) <span class="hljs-keyword">or</span> <span class="hljs-string">""</span> | |
| args = body.get(<span class="hljs-string">"args"</span>) <span class="hljs-keyword">or</span> body.get(<span class="hljs-string">"tool_input"</span>) <span class="hljs-keyword">or</span> body.get(<span class="hljs-string">"prompt"</span>) <span class="hljs-keyword">or</span> <span class="hljs-string">""</span> | |
| <span class="hljs-keyword">return</span> { | |
| <span class="hljs-string">"timestamp"</span>: dt.datetime.utcnow().isoformat(timespec=<span class="hljs-string">"seconds"</span>) + <span class="hljs-string">"Z"</span>, | |
| <span class="hljs-string">"platform"</span>: <span class="hljs-built_in">str</span>(platform), | |
| <span class="hljs-string">"event"</span>: <span class="hljs-built_in">str</span>(event_name), | |
| <span class="hljs-string">"tool"</span>: <span class="hljs-built_in">str</span>(tool), | |
| <span class="hljs-string">"args"</span>: _truncate(args, <span class="hljs-number">200</span>), | |
| } | |
| <span class="hljs-comment"># ---------- FastAPI receiver ----------</span> | |
| api = FastAPI(title=<span class="hljs-string">"Agent Activity Dashboard"</span>) | |
| <span class="hljs-meta">@api.post(<span class="hljs-params"><span class="hljs-string">"/event"</span></span>)</span> | |
| <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">event</span>(<span class="hljs-params">req: Request</span>): | |
| <span class="hljs-keyword">try</span>: | |
| body = <span class="hljs-keyword">await</span> req.json() | |
| <span class="hljs-keyword">except</span> Exception: | |
| body = {} | |
| record = _normalize(body, {k.lower(): v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> req.headers.items()}) | |
| events.appendleft(record) | |
| <span class="hljs-comment"># Return an empty body so hook callers never block on the dashboard receiver.</span> | |
| <span class="hljs-keyword">return</span> JSONResponse({}) | |
| <span class="hljs-meta">@api.get(<span class="hljs-params"><span class="hljs-string">"/health"</span></span>)</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">health</span>(): | |
| <span class="hljs-keyword">return</span> {<span class="hljs-string">"ok"</span>: <span class="hljs-literal">True</span>, <span class="hljs-string">"events"</span>: <span class="hljs-built_in">len</span>(events)} | |
| <span class="hljs-comment"># ---------- Gradio views ----------</span> | |
| COLUMNS = [<span class="hljs-string">"timestamp"</span>, <span class="hljs-string">"platform"</span>, <span class="hljs-string">"event"</span>, <span class="hljs-string">"tool"</span>, <span class="hljs-string">"args"</span>] | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">events_df</span>() -> pd.DataFrame: | |
| <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> events: | |
| <span class="hljs-keyword">return</span> pd.DataFrame(columns=COLUMNS) | |
| <span class="hljs-keyword">return</span> pd.DataFrame(<span class="hljs-built_in">list</span>(events), columns=COLUMNS) | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">tool_counts_df</span>() -> pd.DataFrame: | |
| counter = Counter(e[<span class="hljs-string">"tool"</span>] <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> events <span class="hljs-keyword">if</span> e[<span class="hljs-string">"tool"</span>]) | |
| rows = [{<span class="hljs-string">"tool"</span>: tool, <span class="hljs-string">"count"</span>: n} <span class="hljs-keyword">for</span> tool, n <span class="hljs-keyword">in</span> counter.most_common(<span class="hljs-number">15</span>)] | |
| <span class="hljs-keyword">return</span> pd.DataFrame(rows, columns=[<span class="hljs-string">"tool"</span>, <span class="hljs-string">"count"</span>]) | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">summary_md</span>() -> <span class="hljs-built_in">str</span>: | |
| total = <span class="hljs-built_in">len</span>(events) | |
| platforms = <span class="hljs-built_in">sorted</span>({e[<span class="hljs-string">"platform"</span>] <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> events}) <span class="hljs-keyword">or</span> [<span class="hljs-string">"(none)"</span>] | |
| tools = <span class="hljs-built_in">sorted</span>({e[<span class="hljs-string">"tool"</span>] <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> events <span class="hljs-keyword">if</span> e[<span class="hljs-string">"tool"</span>]}) | |
| tools_display = <span class="hljs-string">", "</span>.join(tools) <span class="hljs-keyword">if</span> tools <span class="hljs-keyword">else</span> <span class="hljs-string">"(none)"</span> | |
| <span class="hljs-keyword">return</span> ( | |
| <span class="hljs-string">f"**Events:** <span class="hljs-subst">{total}</span> (buffer holds up to <span class="hljs-subst">{MAX_EVENTS}</span>) \n"</span> | |
| <span class="hljs-string">f"**Platforms seen:** <span class="hljs-subst">{<span class="hljs-string">', '</span>.join(platforms)}</span> \n"</span> | |
| <span class="hljs-string">f"**Tools seen:** <span class="hljs-subst">{tools_display}</span>"</span> | |
| ) | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">refresh</span>(): | |
| <span class="hljs-keyword">return</span> events_df(), tool_counts_df(), summary_md() | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">clear_events</span>(): | |
| events.clear() | |
| <span class="hljs-keyword">return</span> refresh() | |
| <span class="hljs-keyword">with</span> gr.Blocks(title=<span class="hljs-string">"Agent Activity Dashboard"</span>) <span class="hljs-keyword">as</span> ui: | |
| gr.Markdown(<span class="hljs-string">"# Agent Activity Dashboard"</span>) | |
| gr.Markdown( | |
| <span class="hljs-string">"Point your Claude Code, Codex, OpenCode, or Pi hooks/extensions at "</span> | |
| <span class="hljs-string">"`POST http://localhost:8000/event` to see live activity here."</span> | |
| ) | |
| header = gr.Markdown(value=summary_md()) | |
| <span class="hljs-keyword">with</span> gr.Row(): | |
| clear_btn = gr.Button(<span class="hljs-string">"Clear events"</span>, variant=<span class="hljs-string">"secondary"</span>) | |
| chart = gr.BarPlot( | |
| value=tool_counts_df(), | |
| x=<span class="hljs-string">"tool"</span>, | |
| y=<span class="hljs-string">"count"</span>, | |
| title=<span class="hljs-string">"Tool usage"</span>, | |
| tooltip=[<span class="hljs-string">"tool"</span>, <span class="hljs-string">"count"</span>], | |
| height=<span class="hljs-number">280</span>, | |
| ) | |
| table = gr.Dataframe( | |
| value=events_df(), | |
| headers=COLUMNS, | |
| label=<span class="hljs-string">"Recent events (newest first)"</span>, | |
| wrap=<span class="hljs-literal">True</span>, | |
| interactive=<span class="hljs-literal">False</span>, | |
| ) | |
| <span class="hljs-comment"># Poll the shared buffer once a second.</span> | |
| gr.Timer(<span class="hljs-number">1.0</span>).tick(refresh, outputs=[table, chart, header]) | |
| clear_btn.click(clear_events, outputs=[table, chart, header]) | |
| <span class="hljs-comment"># Mount Gradio on the FastAPI app so `/event` and the UI share one process.</span> | |
| app = gr.mount_gradio_app(api, ui, path=<span class="hljs-string">"/"</span>) | |
| <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>: | |
| <span class="hljs-keyword">import</span> uvicorn | |
| uvicorn.run(app, host=<span class="hljs-string">"0.0.0.0"</span>, port=<span class="hljs-number">8000</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-vg7em0">Run it:</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 -->python app.py<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-dbrytl">Open <code>http://localhost:8000</code> in a browser. The dashboard is empty — that’s expected until a hook sends an event.</p> <blockquote class="tip" data-svelte-h="svelte-1a6web2"><p>The FastAPI route for <code>/event</code> is defined before Gradio is mounted at <code>/</code>, so POSTs hit the receiver and everything else lands on the Gradio UI.</p></blockquote> <h2 class="relative group"><a id="step-2-connect-your-agent" 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-2-connect-your-agent"><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 2: Connect Your Agent</span></h2> <p data-svelte-h="svelte-cthaxr">Use the hook config from the previous lesson. A quick reminder of the shape for each platform, adjusted so the normalizer in <code>app.py</code> can make sense of the payload.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">Claude Code </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Codex </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">OpenCode </div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Pi </div></div> <div class="language-select"><p data-svelte-h="svelte-1mw41u7"><code>.claude/settings.json</code>:</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-punctuation">{</span> | |
| <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"PreToolUse"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"matcher"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"*"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http://localhost:8000/event"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"headers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"X-Platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"claude-code"</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"PostToolUse"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"matcher"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"*"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http://localhost:8000/event"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"headers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"X-Platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"claude-code"</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"UserPromptSubmit"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http://localhost:8000/event"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"headers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"X-Platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"claude-code"</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"Stop"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http://localhost:8000/event"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"headers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"X-Platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"claude-code"</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"SessionStart"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">{</span> <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http://localhost:8000/event"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"headers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"X-Platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"claude-code"</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">]</span> | |
| <span class="hljs-punctuation">}</span> | |
| <span class="hljs-punctuation">}</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-4v31qy">Claude Code’s payload already includes <code>hook_event_name</code>, <code>tool_name</code>, and <code>tool_input</code>, so the normalizer picks them up without extra work. The <code>X-Platform</code> header tags them as <code>claude-code</code>.</p> <p data-svelte-h="svelte-ce1hmd">Start a new Claude Code session in this project directory and ask it to do something concrete:</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 -->List <span class="hljs-keyword">the</span> <span class="hljs-built_in">files</span> <span class="hljs-keyword">in</span> this <span class="hljs-built_in">directory</span>, <span class="hljs-keyword">then</span> <span class="hljs-built_in">read</span> README.md <span class="hljs-keyword">and</span> summarize <span class="hljs-keyword">it</span>.<!-- HTML_TAG_END --></pre></div> </div> <h2 class="relative group"><a id="step-3-watch-it-live" 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-3-watch-it-live"><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 3: Watch It Live</span></h2> <p data-svelte-h="svelte-hgprkk">Leave <code>python app.py</code> running in one terminal and your agent running in another. As the agent works, events stream into the dashboard:</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 -->timestamp platform event tool args | |
| 2026<span class="hljs-string">-04</span><span class="hljs-string">-20</span>T10:15:02Z claude-code UserPromptSubmit List the files… | |
| 2026<span class="hljs-string">-04</span><span class="hljs-string">-20</span>T10:15:03Z claude-code PreToolUse Bash {"command":"ls"} | |
| 2026<span class="hljs-string">-04</span><span class="hljs-string">-20</span>T10:15:03Z claude-code PostToolUse Bash {"exit_code":0,…} | |
| 2026<span class="hljs-string">-04</span><span class="hljs-string">-20</span>T10:15:04Z claude-code PreToolUse Read {"path":"README… | |
| 2026<span class="hljs-string">-04</span><span class="hljs-string">-20</span>T10:15:04Z claude-code PostToolUse Read {"content":"# … | |
| 2026<span class="hljs-string">-04</span><span class="hljs-string">-20</span>T10:15:06Z claude-code Stop …<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-b7u1qp">The bar chart updates live as tools accumulate, which makes it obvious when the agent is stuck in a loop on the same tool.</p> <h2 class="relative group"><a id="step-4-add-a-guardrail" 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-4-add-a-guardrail"><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 4: Add a Guardrail</span></h2> <p data-svelte-h="svelte-1n20nlg">A log-only dashboard is already useful, but the real power of hooks is that they can intervene. Extend the Claude Code hook to block dangerous commands.</p> <p data-svelte-h="svelte-1uhmwr4">Add a second hook handler for <code>PreToolUse</code> that runs before the dashboard logger:</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-punctuation">{</span> | |
| <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"PreToolUse"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> | |
| <span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"matcher"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Bash"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> | |
| <span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"command"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"command"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"jq -r '.tool_input.command // \"\"' | grep -Eq 'rm -rf|:\\(\\)\\{.*\\|.*&.*\\}:' && { echo 'blocked: dangerous shell pattern' >&2; exit 2; } || exit 0"</span> | |
| <span class="hljs-punctuation">}</span> | |
| <span class="hljs-punctuation">]</span> | |
| <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"matcher"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"*"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"hooks"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> | |
| <span class="hljs-punctuation">{</span> <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"http://localhost:8000/event"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"headers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"X-Platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"claude-code"</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span> | |
| <span class="hljs-punctuation">]</span> | |
| <span class="hljs-punctuation">}</span> | |
| <span class="hljs-punctuation">]</span> | |
| <span class="hljs-punctuation">}</span> | |
| <span class="hljs-punctuation">}</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-rtlx51">Now the same <code>PreToolUse</code> event does two things: denies obviously destructive shell patterns (exit code <code>2</code> with a stderr message) and logs everything else to the dashboard. Create a disposable test folder:</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-built_in">mkdir</span> -p /tmp/hook-guardrail-demo-delete-me<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1yjdm92">Then ask the agent to run:</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 -->rm -rf /tmp/hook-guardrail-demo-delete-me<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-gd2dw1">You’ll see it refuse before the command executes, and you’ll see the guardrail event in the dashboard.</p> <blockquote class="tip" data-svelte-h="svelte-jlb7hj"><p>The equivalent OpenCode guard is a <code>throw new Error("blocked: dangerous shell pattern")</code> inside <code>"tool.execute.before"</code>. For Codex, the hook can either exit <code>2</code> with a stderr reason or print <code>jq -c '{hookSpecificOutput:{hookEventName:"PreToolUse", permissionDecision:"deny", permissionDecisionReason:"…"}}'</code> on stdout.</p></blockquote> <h2 class="relative group"><a id="step-5-deploy-optional" 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-5-deploy-optional"><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 5: Deploy (Optional)</span></h2> <p data-svelte-h="svelte-3d0kdp">For a team-facing dashboard, host the Gradio app on <a href="https://huggingface.co/spaces" rel="nofollow">Hugging Face Spaces</a>. Push <code>app.py</code> and <code>requirements.txt</code>, and update the hook URLs from <code>http://localhost:8000/event</code> to <code>https://YOUR-USERNAME-agent-dashboard.hf.space/event</code>.</p> <p data-svelte-h="svelte-1d43rs8">A few cautions for a shared dashboard:</p> <ul data-svelte-h="svelte-rg4p77"><li>Payloads can contain secrets (prompts, file contents, command lines). Redact sensitive fields in <code>_normalize</code> before appending to the buffer.</li> <li>A public Space is, well, public. Put authentication in front of <code>/event</code>, or keep the Space private and use a personal access token in the hook <code>headers</code>.</li> <li>The in-memory buffer resets on restart. For durable history, swap <code>deque</code> for a SQLite file or a Spaces persistent volume.</li></ul> <h2 class="relative group"><a id="full-directory-layout" 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="#full-directory-layout"><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>Full Directory Layout</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 -->agent-activity-dashboard/ | |
| ├── app.py <span class="hljs-comment"># Gradio + FastAPI server</span> | |
| ├── requirements.txt | |
| ├── <span class="hljs-string">.claude/</span> | |
| │ └── settings.json <span class="hljs-comment"># Claude Code hooks</span> | |
| ├── <span class="hljs-string">.codex/</span> | |
| │ └── hooks.json <span class="hljs-comment"># Codex hooks</span> | |
| ├── <span class="hljs-string">.opencode/</span> | |
| │ └── plugins/ | |
| │ └── dashboard.ts <span class="hljs-comment"># OpenCode plugin</span> | |
| └── <span class="hljs-string">.pi/</span> | |
| └── extensions/ | |
| └── dashboard.ts <span class="hljs-comment"># Pi extension</span><!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="best-practices-demonstrated" 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="#best-practices-demonstrated"><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>Best Practices Demonstrated</span></h2> <p data-svelte-h="svelte-1sruzmf">This project shows how to pick the right event for observability (<code>PreToolUse</code> and <code>PostToolUse</code>), how to collapse four different payload shapes into one normalized record, and how to use exit codes / <code>hookSpecificOutput</code> / thrown errors / returned values to enforce policy. The Gradio + FastAPI pattern — one process, two surfaces — keeps the whole thing short and makes it easy to host on Spaces.</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-1yg7kbu">A hook is only useful if its output goes somewhere. A Gradio dashboard is a fast way to turn raw hook events into something you can actually learn from: you can watch an agent work, see where it’s stuck, and prove that guardrails fire. The same dashboard works across Claude Code, Codex, OpenCode, and Pi because the event shape is narrow and the normalizer is small.</p> <p data-svelte-h="svelte-1ie4ifs">Next, one more quiz to wrap up the unit.</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/unit5/hands-on.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, 30], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 66 kB
- Xet hash:
- 475479fdd65283353f04228fe6a25db25c9fe4af3686a86d32ccf93944d82b6a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.