{"slug": "i-built-a-desktop-cat-that-watches-claude-code-and-the-hooks-that-make-it-work", "title": "I built a desktop cat that watches Claude Code (and the hooks that make it work)", "summary": "A developer built Mochi, a desktop pixel-cat companion that monitors Claude Code sessions using the tool's lifecycle hooks, narrating agent activity and chiming when the agent needs permission. The hook script forwards slimmed JSON payloads to a local server on port 4777, deliberately exiting cleanly and never blocking Claude, since hook stdout on UserPromptSubmit and SessionStart is injected into the model's context and can burn tokens.", "body_md": "I kept doing the same thing with Claude Code: start a task, switch to another window, and come back five minutes later to find it had been sitting on a permission prompt the whole time.\n\nSo I built a small pixel cat that sits on my desktop and watches the agent for me. She narrates what Claude is doing, jumps and chimes when it needs my OK, and gets sleepy when the context window fills up. Everything runs on Claude Code's **hooks**, which turned out to be a really nice API for this, so here's how the plumbing works in case you want to build your own.\n\nClaude Code can run a shell command at points in its lifecycle. You register them in `~/.claude/settings.json`:\n\n```\n{\n  \"hooks\": {\n    \"PreToolUse\": [\n      { \"matcher\": \"*\", \"hooks\": [{ \"type\": \"command\", \"command\": \"node ~/.mochi/app/hook.js\", \"timeout\": 5 }] }\n    ],\n    \"Notification\":     [{ \"hooks\": [{ \"type\": \"command\", \"command\": \"node ~/.mochi/app/hook.js\" }] }],\n    \"Stop\":             [{ \"hooks\": [{ \"type\": \"command\", \"command\": \"node ~/.mochi/app/hook.js\" }] }],\n    \"UserPromptSubmit\": [{ \"hooks\": [{ \"type\": \"command\", \"command\": \"node ~/.mochi/app/hook.js\" }] }],\n    \"SessionStart\":     [{ \"hooks\": [{ \"type\": \"command\", \"command\": \"node ~/.mochi/app/hook.js\" }] }]\n  }\n}\n```\n\nEach run gets a JSON payload on **stdin**. The fields I ended up using:\n\n| Field | What it tells you | \n|---|---|\n| `hook_event_name` | `PreToolUse` ,`Notification` ,`Stop` , ... | \n| `tool_name` ,`tool_input` | e.g. `Bash` +`{ \"command\": \"npm test\" }` , or`Read` +`{ \"file_path\": \"src/auth.ts\" }` | \n| `cwd` | the project folder | \n| `message` | for `Notification` , e.g. \"Claude needs your permission to use Bash\" | \n| `transcript_path` | the session's JSONL transcript (more on this below) | \n\nThat's enough to map the agent's state onto a pet:\n\n| Event | Mood | \n|---|---|\n| `UserPromptSubmit` ,`PreToolUse` | working | \n| `Notification` | needs you (jump + chime) | \n| `Stop` | done (confetti) | \n| nothing for 5 minutes | napping | \n\nThis was the most important design constraint. A hook that hangs or prints the wrong thing makes the agent worse, so mine:\n\n`UserPromptSubmit` and `SessionStart`), whatever a hook writes to stdout gets added to Claude's context. A chatty hook would quietly burn your tokens.\n\n``` js\n// hook.js: forward the event to the pet, never block Claude\nconst http = require('http');\nlet input = '';\nprocess.stdin.on('data', d => (input += d));\nprocess.stdin.on('end', () => {\n  const ev = JSON.parse(input || '{}');\n  const ti = ev.tool_input || {};\n  const slim = JSON.stringify({            // send only what the pet needs\n    hook_event_name: ev.hook_event_name, cwd: ev.cwd, tool_name: ev.tool_name,\n    message: ev.message, transcript_path: ev.transcript_path,\n    tool_input: { file_path: ti.file_path, command: (ti.command || '').slice(0, 200), pattern: ti.pattern },\n  });\n  const req = http.request({ host: '127.0.0.1', port: 4777, path: '/hook', method: 'POST', timeout: 700,\n    headers: { 'content-type': 'application/json' } }, res => { res.resume(); res.on('end', () => process.exit(0)); });\n  req.on('error', () => process.exit(0));\n  req.on('timeout', () => { req.destroy(); process.exit(0); });\n  req.end(slim);\n});\nsetTimeout(() => process.exit(0), 2500).unref();   // hard stop no matter what\n```\n\nNote the \"slim\" payload: the pet never sees file contents, just names.\n\n`PreToolUse` fires before every tool call, so a tiny mapper gives you a running commentary:\n\n``` js\nfunction narrate(tool, inp = {}) {\n  const base = p => (p || '').split('/').pop();\n  switch (tool) {\n    case 'Read':  return `reading ${base(inp.file_path)}`;\n    case 'Edit':  return `editing ${base(inp.file_path)}`;\n    case 'Grep':  return `searching \"${inp.pattern}\"`;\n    case 'Bash':\n      if (/\\b(test|jest|pytest|vitest)\\b/.test(inp.command)) return 'running tests';\n      if (/\\bgit\\s+commit\\b/.test(inp.command))             return 'committing';\n      return `$ ${inp.command.split(/\\s+/).slice(0, 2).join(' ')}`;\n    default: return tool.startsWith('mcp__') ? `using ${tool.split('__')[1]}` : tool.toLowerCase();\n  }\n}\n```\n\nOnly the first two words of a shell command are shown, so secrets in arguments don't end up in a speech bubble.\n\nEvery hook payload includes `transcript_path`, the JSONL file of the session. Assistant turns in it carry a `usage` block, so you can read the tail of the file, find the last one and add up the input-side tokens:\n\n```\nfunction contextTokens(transcriptPath) {\n  const { size } = fs.statSync(transcriptPath);\n  const n = Math.min(size, 400_000), buf = Buffer.alloc(n), fd = fs.openSync(transcriptPath, 'r');\n  fs.readSync(fd, buf, 0, n, size - n); fs.closeSync(fd);\n  for (const line of buf.toString('utf8').split('\\n').reverse()) {\n    if (!line.includes('\"usage\"')) continue;\n    try {\n      const u = JSON.parse(line).message?.usage;\n      if (u) return u.input_tokens + (u.cache_read_input_tokens || 0) + (u.cache_creation_input_tokens || 0);\n    } catch {}\n  }\n  return null;\n}\n```\n\nDivide by your context window (200k, or 1M if you use a 1M model) and you get a percentage. The cat starts drooping past ~70% and suggests `/compact`. It turned out to be the feature I use most: it's a passive reminder that the session is getting expensive and slow *before* it auto-compacts at a bad moment.\n\nIf you build something like this, two cheap protections go a long way:\n\n`127.0.0.1` and check the `Host` header`x-mochi: 1`). Browsers can't send custom headers cross-origin without a CORS preflight, and if you never answer the preflight, websites simply can't post fake events.\nThe hooks + a tiny Node server became **Mochi**, a Mac app with:\n\n`claude -p`, so no API key is needed)\nIt's **$3.99 at launch**, or **$14.99 with the full source** if you want to build your own pet on top of it. It's also free in the [Mega Pack](https://samyakvaghela.gumroad.com/l/claude-code-bundle) with my Claude Code skill tree.\n\nBut honestly, the hook script above is the whole trick. If you build something with it, a Pomodoro timer or a Slack ping or a lamp that turns red when Claude needs you, I'd love to see it in the comments.", "url": "https://wpnews.pro/news/i-built-a-desktop-cat-that-watches-claude-code-and-the-hooks-that-make-it-work", "canonical_source": "https://dev.to/samyak_vaghela_ff0e1e6dd8/i-built-a-desktop-cat-that-watches-claude-code-and-the-hooks-that-make-it-work-43li", "published_at": "2026-12-02 18:10:13+00:00", "updated_at": "2026-12-02 18:18:13.147446+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "large-language-models"], "entities": ["Claude Code", "Anthropic", "Mochi"], "also_reported_by": [], "takeaway": "Claude Code hooks that print to stdout on UserPromptSubmit or SessionStart inject that output into the model's context, so hook scripts should forward only slimmed fields to a local server and always exit 0 with a hard timeout to avoid blocking the agent.", "recommendation": {"advice": "When wiring Claude Code hooks, keep stdout silent on context-injecting events, forward only needed fields, and add a hard timeout with exit 0 so a hung hook never stalls the agent.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/i-built-a-desktop-cat-that-watches-claude-code-and-the-hooks-that-make-it-work", "markdown": "https://wpnews.pro/news/i-built-a-desktop-cat-that-watches-claude-code-and-the-hooks-that-make-it-work.md", "text": "https://wpnews.pro/news/i-built-a-desktop-cat-that-watches-claude-code-and-the-hooks-that-make-it-work.txt", "jsonld": "https://wpnews.pro/news/i-built-a-desktop-cat-that-watches-claude-code-and-the-hooks-that-make-it-work.jsonld"}}