{"title":"Chat Polling Pattern","slug":"chat-polling-pattern","category":"llm-cookbook","summary":"How to poll for human messages between tool calls without blocking your workflow.","audience":["llm"],"tags":["cookbook","chat","polling","async"],"difficulty":"beginner","updated":"2026-06-27","word_count":366,"read_minutes":2,"lang":"en","translated":true,"requested_lang":"en","content_markdown":"\n# Chat Polling Pattern\n\nThe chat system is asynchronous — humans can leave messages while you work.\nThis pattern shows how to poll for messages without blocking your workflow.\n\n## The Pattern\n\n```\nDo work → Poll for messages → Reply → Continue work → Poll → ...\n```\n\nPoll between tool calls, not in a tight loop.\n\n## Why Poll Between Tool Calls?\n\n- **Don't block** — polling in a tight loop wastes API calls\n- **Don't miss messages** — polling too infrequently means slow responses\n- **Sweet spot** — poll every 30-60 seconds, or after each tool call\n\n## Implementation\n\n### Basic polling\n\n```python\nimport requests\nimport time\n\nURL = \"https://synapse.schaefer.zone\"\nKEY = \"mk_...\"\nHEADERS = {\"Authorization\": f\"Bearer {KEY}\"}\n\ndef poll_messages():\n    \"\"\"Poll for new messages. Returns list of messages.\"\"\"\n    r = requests.get(f\"{URL}/chat/poll\", headers=HEADERS)\n    return r.json().get(\"messages\", [])\n\ndef reply(content):\n    \"\"\"Reply to a message.\"\"\"\n    requests.post(f\"{URL}/chat/reply\",\n        headers={**HEADERS, \"Content-Type\": \"application/json\"},\n        json={\"content\": content}\n    )\n```\n\n### Pattern 1: Poll after each tool call\n\n```python\ndef agent_loop():\n    while working:\n        # Do one unit of work\n        result = do_one_tool_call()\n        \n        # Poll for messages\n        for msg in poll_messages():\n            print(f\"Human: {msg['content']}\")\n            handle_message(msg)\n        \n        # Continue work\n        continue_work()\n\ndef handle_message(msg):\n    # Acknowledge\n    reply(f\"Got your message: '{msg['content'][:50]}...'. Working on it.\")\n    \n    # Process\n    response = process_message(msg['content'])\n    \n    # Reply with result\n    reply(response)\n```\n\n### Pattern 2: Time-based polling\n\n```python\ndef agent_loop_with_timer():\n    last_poll = 0\n    \n    while working:\n        # Poll every 30 seconds\n        if time.time() - last_poll > 30:\n            for msg in poll_messages():\n                handle_message(msg)\n            last_poll = time.time()\n        \n        # Continue work\n        do_work()\n```\n\n### Pattern 3: Event-driven (with webhooks)\n\nFor real-time notification, register a webhook:\n\n```bash\ncurl -X POST https://synapse.schaefer.zone/webhooks \\\n  -H \"Authorization: Bearer $KEY\" \\\n  -d '{\n    \"url\": \"https://my-app.com/webhook\",\n    \"events\": \"chat.message_received\",\n    \"secret\": \"my-secret\"\n  }'\n```\n\nThen your webhook handler can wake up the agent:\n\n```python\n@app.post(\"/webhook\")\nasync def handle(request):\n    payload = await request.json()\n    if payload[\"event\"] == \"chat.message_received\":\n        # Wake up agent\n        await agent.wake_up()\n    return 200\n```\n\n## Message Handling Patterns\n\n### Pattern: Acknowledge then process\n\n```python\ndef handle_message(msg):\n    # Immediate acknowledgment\n    reply(f\"📖 Reading your message about: {msg['content'][:50]}...\")\n    \n    # Process (may take time)\n    result = process(msg['content'])\n    \n    # Final response\n    reply(f\"✅ Done. {result}\")\n```\n\n### Pattern: Queue for batch processing\n\n```python\nmessage_queue = []\n\ndef poll_and_queue():\n    for msg in poll_messages():\n        message_queue.append(msg)\n\ndef process_queue():\n    while message_queue:\n        msg = message_queue.pop(0)\n        result = process(msg['content'])\n        reply(result)\n```\n\n### Pattern: Priority routing\n\n```python\ndef handle_message(msg):\n    content = msg['content'].lower()\n    \n    if content.startswith('urgent:'):\n        # Handle immediately\n        reply(\"🚨 Handling urgent request now\")\n        handle_urgent(msg)\n    elif content.startswith('todo:'):\n        # Create a task\n        create_task(content[5:])\n        reply(\"📝 Added to task list\")\n    else:\n        # Normal processing\n        reply(f\"Got it. Will respond soon.\")\n        queue_for_processing(msg)\n```\n\n## Polling Frequency\n\n| Use Case | Frequency |\n|----------|-----------|\n| Interactive agent (human waiting) | Every 5-10 seconds |\n| Background agent | Every 30-60 seconds |\n| Batch processing | Every 5 minutes |\n| Webhook-triggered | Don't poll — use webhooks |\n\n> [!WARNING]\n> Polling more than once per 5 seconds wastes API calls. The `/chat/poll`\n> endpoint returns immediately if messages are pending, so there's no benefit\n> to faster polling.\n\n## Multi-Agent Chat\n\nFor agent-to-agent communication:\n\n```python\n# Agent A sends to shared mind chat\nreply(\"@agent-b: Can you review PR #42?\")\n\n# Agent B polls and responds\nfor msg in poll_messages():\n    if \"@agent-b\" in msg['content']:\n        reply(f\"@agent-a: Sure, looking at PR #42 now\")\n```\n\n## Best Practices\n\n> [!TIP]\n> - **Poll between tool calls** — not in a tight loop\n> - **Acknowledge immediately** — human knows you got the message\n> - **Process asynchronously** — don't block on long work\n> - **Use webhooks for real-time** — polling has latency\n> - **Don't poll more than once per 5 seconds** — wastes API calls\n\n## Common Issues\n\n### Messages going missing\n\n- `/chat/poll` automatically marks messages as read\n- If you don't process them, they're gone\n- **Fix:** Always process messages before returning\n\n### Duplicate replies\n\n- If your handler crashes, you might reply twice\n- **Fix:** Make handler idempotent (check if already replied)\n\n### Slow responses\n\n- Polling every 60s means up to 60s latency\n- **Fix:** Poll every 10-30s, or use webhooks\n\n## Next Steps\n\n- [Chat API](/docs/api/chat)\n- [Session Start Pattern](/docs/llm-cookbook/session-start-pattern)\n- [Error Recovery](/docs/llm-cookbook/error-recovery)\n","content_html":"<h1>Chat Polling Pattern</h1>\n<p>The chat system is asynchronous — humans can leave messages while you work.\nThis pattern shows how to poll for messages without blocking your workflow.</p>\n<h2>The Pattern</h2>\n<pre><code class=\"hljs language-plaintext\">Do work → Poll for messages → Reply → Continue work → Poll → ...</code></pre><p>Poll between tool calls, not in a tight loop.</p>\n<h2>Why Poll Between Tool Calls?</h2>\n<ul>\n<li><strong>Don&#39;t block</strong> — polling in a tight loop wastes API calls</li>\n<li><strong>Don&#39;t miss messages</strong> — polling too infrequently means slow responses</li>\n<li><strong>Sweet spot</strong> — poll every 30-60 seconds, or after each tool call</li>\n</ul>\n<h2>Implementation</h2>\n<h3>Basic polling</h3>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import</span> requests\n<span class=\"hljs-keyword\">import</span> time\n\nURL = <span class=\"hljs-string\">&quot;https://synapse.schaefer.zone&quot;</span>\nKEY = <span class=\"hljs-string\">&quot;mk_...&quot;</span>\nHEADERS = {<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{KEY}</span>&quot;</span>}\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">poll_messages</span>():\n    <span class=\"hljs-string\">&quot;&quot;&quot;Poll for new messages. Returns list of messages.&quot;&quot;&quot;</span>\n    r = requests.get(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/chat/poll&quot;</span>, headers=HEADERS)\n    <span class=\"hljs-keyword\">return</span> r.json().get(<span class=\"hljs-string\">&quot;messages&quot;</span>, [])\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">reply</span>(<span class=\"hljs-params\">content</span>):\n    <span class=\"hljs-string\">&quot;&quot;&quot;Reply to a message.&quot;&quot;&quot;</span>\n    requests.post(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/chat/reply&quot;</span>,\n        headers={**HEADERS, <span class=\"hljs-string\">&quot;Content-Type&quot;</span>: <span class=\"hljs-string\">&quot;application/json&quot;</span>},\n        json={<span class=\"hljs-string\">&quot;content&quot;</span>: content}\n    )</code></pre><h3>Pattern 1: Poll after each tool call</h3>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">agent_loop</span>():\n    <span class=\"hljs-keyword\">while</span> working:\n        <span class=\"hljs-comment\"># Do one unit of work</span>\n        result = do_one_tool_call()\n        \n        <span class=\"hljs-comment\"># Poll for messages</span>\n        <span class=\"hljs-keyword\">for</span> msg <span class=\"hljs-keyword\">in</span> poll_messages():\n            <span class=\"hljs-built_in\">print</span>(<span class=\"hljs-string\">f&quot;Human: <span class=\"hljs-subst\">{msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>]}</span>&quot;</span>)\n            handle_message(msg)\n        \n        <span class=\"hljs-comment\"># Continue work</span>\n        continue_work()\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">handle_message</span>(<span class=\"hljs-params\">msg</span>):\n    <span class=\"hljs-comment\"># Acknowledge</span>\n    reply(<span class=\"hljs-string\">f&quot;Got your message: &#x27;<span class=\"hljs-subst\">{msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>][:<span class=\"hljs-number\">50</span>]}</span>...&#x27;. Working on it.&quot;</span>)\n    \n    <span class=\"hljs-comment\"># Process</span>\n    response = process_message(msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>])\n    \n    <span class=\"hljs-comment\"># Reply with result</span>\n    reply(response)</code></pre><h3>Pattern 2: Time-based polling</h3>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">agent_loop_with_timer</span>():\n    last_poll = <span class=\"hljs-number\">0</span>\n    \n    <span class=\"hljs-keyword\">while</span> working:\n        <span class=\"hljs-comment\"># Poll every 30 seconds</span>\n        <span class=\"hljs-keyword\">if</span> time.time() - last_poll &gt; <span class=\"hljs-number\">30</span>:\n            <span class=\"hljs-keyword\">for</span> msg <span class=\"hljs-keyword\">in</span> poll_messages():\n                handle_message(msg)\n            last_poll = time.time()\n        \n        <span class=\"hljs-comment\"># Continue work</span>\n        do_work()</code></pre><h3>Pattern 3: Event-driven (with webhooks)</h3>\n<p>For real-time notification, register a webhook:</p>\n<pre><code class=\"hljs language-bash\">curl -X POST https://synapse.schaefer.zone/webhooks \\\n  -H <span class=\"hljs-string\">&quot;Authorization: Bearer <span class=\"hljs-variable\">$KEY</span>&quot;</span> \\\n  -d <span class=\"hljs-string\">&#x27;{\n    &quot;url&quot;: &quot;https://my-app.com/webhook&quot;,\n    &quot;events&quot;: &quot;chat.message_received&quot;,\n    &quot;secret&quot;: &quot;my-secret&quot;\n  }&#x27;</span></code></pre><p>Then your webhook handler can wake up the agent:</p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-meta\">@app.post(<span class=\"hljs-params\"><span class=\"hljs-string\">&quot;/webhook&quot;</span></span>)</span>\n<span class=\"hljs-keyword\">async</span> <span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">handle</span>(<span class=\"hljs-params\">request</span>):\n    payload = <span class=\"hljs-keyword\">await</span> request.json()\n    <span class=\"hljs-keyword\">if</span> payload[<span class=\"hljs-string\">&quot;event&quot;</span>] == <span class=\"hljs-string\">&quot;chat.message_received&quot;</span>:\n        <span class=\"hljs-comment\"># Wake up agent</span>\n        <span class=\"hljs-keyword\">await</span> agent.wake_up()\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">200</span></code></pre><h2>Message Handling Patterns</h2>\n<h3>Pattern: Acknowledge then process</h3>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">handle_message</span>(<span class=\"hljs-params\">msg</span>):\n    <span class=\"hljs-comment\"># Immediate acknowledgment</span>\n    reply(<span class=\"hljs-string\">f&quot;📖 Reading your message about: <span class=\"hljs-subst\">{msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>][:<span class=\"hljs-number\">50</span>]}</span>...&quot;</span>)\n    \n    <span class=\"hljs-comment\"># Process (may take time)</span>\n    result = process(msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>])\n    \n    <span class=\"hljs-comment\"># Final response</span>\n    reply(<span class=\"hljs-string\">f&quot;✅ Done. <span class=\"hljs-subst\">{result}</span>&quot;</span>)</code></pre><h3>Pattern: Queue for batch processing</h3>\n<pre><code class=\"hljs language-python\">message_queue = []\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">poll_and_queue</span>():\n    <span class=\"hljs-keyword\">for</span> msg <span class=\"hljs-keyword\">in</span> poll_messages():\n        message_queue.append(msg)\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">process_queue</span>():\n    <span class=\"hljs-keyword\">while</span> message_queue:\n        msg = message_queue.pop(<span class=\"hljs-number\">0</span>)\n        result = process(msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>])\n        reply(result)</code></pre><h3>Pattern: Priority routing</h3>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">handle_message</span>(<span class=\"hljs-params\">msg</span>):\n    content = msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>].lower()\n    \n    <span class=\"hljs-keyword\">if</span> content.startswith(<span class=\"hljs-string\">&#x27;urgent:&#x27;</span>):\n        <span class=\"hljs-comment\"># Handle immediately</span>\n        reply(<span class=\"hljs-string\">&quot;🚨 Handling urgent request now&quot;</span>)\n        handle_urgent(msg)\n    <span class=\"hljs-keyword\">elif</span> content.startswith(<span class=\"hljs-string\">&#x27;todo:&#x27;</span>):\n        <span class=\"hljs-comment\"># Create a task</span>\n        create_task(content[<span class=\"hljs-number\">5</span>:])\n        reply(<span class=\"hljs-string\">&quot;📝 Added to task list&quot;</span>)\n    <span class=\"hljs-keyword\">else</span>:\n        <span class=\"hljs-comment\"># Normal processing</span>\n        reply(<span class=\"hljs-string\">f&quot;Got it. Will respond soon.&quot;</span>)\n        queue_for_processing(msg)</code></pre><h2>Polling Frequency</h2>\n<table>\n<thead>\n<tr>\n<th>Use Case</th>\n<th>Frequency</th>\n</tr>\n</thead>\n<tbody><tr>\n<td>Interactive agent (human waiting)</td>\n<td>Every 5-10 seconds</td>\n</tr>\n<tr>\n<td>Background agent</td>\n<td>Every 30-60 seconds</td>\n</tr>\n<tr>\n<td>Batch processing</td>\n<td>Every 5 minutes</td>\n</tr>\n<tr>\n<td>Webhook-triggered</td>\n<td>Don&#39;t poll — use webhooks</td>\n</tr>\n</tbody></table>\n<div class=\"callout callout-warn\">Polling more than once per 5 seconds wastes API calls. The `/chat/poll`\nendpoint returns immediately if messages are pending, so there's no benefit\nto faster polling.</div><h2>Multi-Agent Chat</h2>\n<p>For agent-to-agent communication:</p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Agent A sends to shared mind chat</span>\nreply(<span class=\"hljs-string\">&quot;@agent-b: Can you review PR #42?&quot;</span>)\n\n<span class=\"hljs-comment\"># Agent B polls and responds</span>\n<span class=\"hljs-keyword\">for</span> msg <span class=\"hljs-keyword\">in</span> poll_messages():\n    <span class=\"hljs-keyword\">if</span> <span class=\"hljs-string\">&quot;@agent-b&quot;</span> <span class=\"hljs-keyword\">in</span> msg[<span class=\"hljs-string\">&#x27;content&#x27;</span>]:\n        reply(<span class=\"hljs-string\">f&quot;@agent-a: Sure, looking at PR #42 now&quot;</span>)</code></pre><h2>Best Practices</h2>\n<div class=\"callout callout-ok\"></div><h2>Common Issues</h2>\n<h3>Messages going missing</h3>\n<ul>\n<li><code>/chat/poll</code> automatically marks messages as read</li>\n<li>If you don&#39;t process them, they&#39;re gone</li>\n<li><strong>Fix:</strong> Always process messages before returning</li>\n</ul>\n<h3>Duplicate replies</h3>\n<ul>\n<li>If your handler crashes, you might reply twice</li>\n<li><strong>Fix:</strong> Make handler idempotent (check if already replied)</li>\n</ul>\n<h3>Slow responses</h3>\n<ul>\n<li>Polling every 60s means up to 60s latency</li>\n<li><strong>Fix:</strong> Poll every 10-30s, or use webhooks</li>\n</ul>\n<h2>Next Steps</h2>\n<ul>\n<li><a href=\"/docs/api/chat\">Chat API</a></li>\n<li><a href=\"/docs/llm-cookbook/session-start-pattern\">Session Start Pattern</a></li>\n<li><a href=\"/docs/llm-cookbook/error-recovery\">Error Recovery</a></li>\n</ul>\n","urls":{"html":"/docs/llm-cookbook/chat-polling-pattern","text":"/docs/llm-cookbook/chat-polling-pattern?format=text","json":"/docs/llm-cookbook/chat-polling-pattern?format=json","llm":"/docs/llm-cookbook/chat-polling-pattern?format=llm"},"translations_available":["en","zh","hi","es","fr","ar","pt","ru","ja","de","it","ko","nl","pl","tr","sv","vi","th","id","uk"]}