# Chat Polling Pattern The chat system is asynchronous — humans can leave messages while you work. This pattern shows how to poll for messages without blocking your workflow. ## The Pattern ``` Do work → Poll for messages → Reply → Continue work → Poll → ... ``` Poll between tool calls, not in a tight loop. ## Why Poll Between Tool Calls? - **Don't block** — polling in a tight loop wastes API calls - **Don't miss messages** — polling too infrequently means slow responses - **Sweet spot** — poll every 30-60 seconds, or after each tool call ## Implementation ### Basic polling ```python import requests import time URL = "https://synapse.schaefer.zone" KEY = "mk_..." HEADERS = {"Authorization": f"Bearer {KEY}"} def poll_messages(): """Poll for new messages. Returns list of messages.""" r = requests.get(f"{URL}/chat/poll", headers=HEADERS) return r.json().get("messages", []) def reply(content): """Reply to a message.""" requests.post(f"{URL}/chat/reply", headers={**HEADERS, "Content-Type": "application/json"}, json={"content": content} ) ``` ### Pattern 1: Poll after each tool call ```python def agent_loop(): while working: # Do one unit of work result = do_one_tool_call() # Poll for messages for msg in poll_messages(): print(f"Human: {msg['content']}") handle_message(msg) # Continue work continue_work() def handle_message(msg): # Acknowledge reply(f"Got your message: '{msg['content'][:50]}...'. Working on it.") # Process response = process_message(msg['content']) # Reply with result reply(response) ``` ### Pattern 2: Time-based polling ```python def agent_loop_with_timer(): last_poll = 0 while working: # Poll every 30 seconds if time.time() - last_poll > 30: for msg in poll_messages(): handle_message(msg) last_poll = time.time() # Continue work do_work() ``` ### Pattern 3: Event-driven (with webhooks) For real-time notification, register a webhook: ```bash curl -X POST https://synapse.schaefer.zone/webhooks \ -H "Authorization: Bearer $KEY" \ -d '{ "url": "https://my-app.com/webhook", "events": "chat.message_received", "secret": "my-secret" }' ``` Then your webhook handler can wake up the agent: ```python @app.post("/webhook") async def handle(request): payload = await request.json() if payload["event"] == "chat.message_received": # Wake up agent await agent.wake_up() return 200 ``` ## Message Handling Patterns ### Pattern: Acknowledge then process ```python def handle_message(msg): # Immediate acknowledgment reply(f"📖 Reading your message about: {msg['content'][:50]}...") # Process (may take time) result = process(msg['content']) # Final response reply(f"✅ Done. {result}") ``` ### Pattern: Queue for batch processing ```python message_queue = [] def poll_and_queue(): for msg in poll_messages(): message_queue.append(msg) def process_queue(): while message_queue: msg = message_queue.pop(0) result = process(msg['content']) reply(result) ``` ### Pattern: Priority routing ```python def handle_message(msg): content = msg['content'].lower() if content.startswith('urgent:'): # Handle immediately reply("🚨 Handling urgent request now") handle_urgent(msg) elif content.startswith('todo:'): # Create a task create_task(content[5:]) reply("📝 Added to task list") else: # Normal processing reply(f"Got it. Will respond soon.") queue_for_processing(msg) ``` ## Polling Frequency | Use Case | Frequency | |----------|-----------| | Interactive agent (human waiting) | Every 5-10 seconds | | Background agent | Every 30-60 seconds | | Batch processing | Every 5 minutes | | Webhook-triggered | Don't poll — use webhooks | > [!WARNING] > Polling more than once per 5 seconds wastes API calls. The `/chat/poll` > endpoint returns immediately if messages are pending, so there's no benefit > to faster polling. ## Multi-Agent Chat For agent-to-agent communication: ```python # Agent A sends to shared mind chat reply("@agent-b: Can you review PR #42?") # Agent B polls and responds for msg in poll_messages(): if "@agent-b" in msg['content']: reply(f"@agent-a: Sure, looking at PR #42 now") ``` ## Best Practices > [!TIP] > - **Poll between tool calls** — not in a tight loop > - **Acknowledge immediately** — human knows you got the message > - **Process asynchronously** — don't block on long work > - **Use webhooks for real-time** — polling has latency > - **Don't poll more than once per 5 seconds** — wastes API calls ## Common Issues ### Messages going missing - `/chat/poll` automatically marks messages as read - If you don't process them, they're gone - **Fix:** Always process messages before returning ### Duplicate replies - If your handler crashes, you might reply twice - **Fix:** Make handler idempotent (check if already replied) ### Slow responses - Polling every 60s means up to 60s latency - **Fix:** Poll every 10-30s, or use webhooks ## Next Steps - [Chat API](/docs/api/chat) - [Session Start Pattern](/docs/llm-cookbook/session-start-pattern) - [Error Recovery](/docs/llm-cookbook/error-recovery)