Duyetbot Agent
Concepts

Batching Strategies

Optimize window, trim history, cap maxMsgs. Reduce LLM calls 55%.

TL;DR: Set windowMs:500. Trim history to 10 msgs. Cap maxMessages:10. Recover stuck via heartbeats.

Table of Contents

Config Table

Tune batch-types.ts defaults.

ConfigDefaultFast UXMax SavingsImpact
windowMs5001001000Latency vs batch size
maxWindowMs5000200010000Max collection time
maxMessages10520Prevent overload

Test impacts: bun test --filter batch.

Trim History

Keep recent messages. Use history.ts.

// packages/cloudflare-agent/src/history.ts
export function trimHistory(messages: Message[], maxLength: number): Message[] {
  if (messages.length <= maxLength) {
    return messages;
  }
  return messages.slice(-maxLength);  // Keep last N
}

Call before LLM: trimHistory(state.messages, 10).

Window Tuning

Short window: Fast replies, less batching.

Long window: More savings, higher latency.

Imperative: Start with 500ms. Measure batch sizes in logs.

From batch-types.ts:

// Check if ready to process
if (shouldProcessImmediately(state, config)) {
  // Promote pending -> active
}

Decision Tree

+----------+
| New Msg  |
+-----+----+
      |
      v
+--------------+
| activeBatch? |
+-----+----+---+
      |    |
    Yes   No
      |    |
      v    v
+-----------+  +------------------+
| Stuck 30s?|  | Start pending    |
+---+----+-+  | Alarm 500ms      |
   Yes   No   +-------+----------+
    |    |           |
    v    v           v
 +-----+ +-------+ +--------------+
 |Clear| |Add to | | onBatchAlarm:|
 |    | |pending| |pending->active
 +-----+ +-------+ +-----------+---+
                               |
                               v
                      +-----------+
                      |Process LLM|
                      +-----------+

Integrates batching-alarms.md.

Stuck Quiz

Q: Recover stuck batch when?

A: Heartbeat >30s stale ✅
B: Always retry
C: Manual only

Run bun run deploy:telegram. Send rapid msgs. Check logs for batch sizes!

On this page