Overlapping Messages
When a message arrives on a thread while a handler is still processing an earlier one, the SDK drops the new message by default. The concurrency option on ChatConfig lets you choose a different strategy.
Strategies
Drop (default)
This is the original behavior. If a handler is already running on a thread, the new message is discarded and a LockError is thrown. Nothing is queued or retried.
Queue
Messages that arrive while a handler is running are enqueued. When the current handler finishes, only the latest queued message is dispatched. The intermediate messages are passed in context.skipped, so your handler can see everything that arrived while it was busy.
Flow:
Burst
Waits for debounceMs before the first handler on an idle thread, then drains the collected burst like queue. The latest message is dispatched, and earlier messages in the burst are available as context.skipped.
Use this for assistant-style bots where users often send one logical turn as several short messages inside a small window and you want one response with full context. Compared to debounce, burst keeps the earlier messages in context.skipped instead of dropping them, and it flushes queued messages that arrive while the handler is running.
Choose debounce when the latest message replaces earlier ones, like rapid corrections. Choose burst when earlier messages still matter, like "hey", "quick question", and then the actual question. The tradeoff is that even a lone message waits for debounceMs before the handler runs.
Flow:
Debounce
The first message waits for debounceMs. Messages that arrive during that window replace the pending message, so only the final message in the window is processed.
This helps on platforms like WhatsApp and Telegram, where users tend to send several short messages in quick succession instead of composing one. "hey", "quick question", and "how do I reset my password?" can arrive as three separate webhooks within a few seconds. Without debounce, the bot would respond to "hey" before the actual question even arrives. With debounce, the SDK waits briefly and processes only the final message in the window.
WhatsApp and Telegram adapters default to lockScope: "channel", so debounce applies to the entire conversation rather than a single thread.
Flow:
Debounce also works well for rapid corrections ("wait, I meant...") and multi-part messages on any platform.
Concurrent
No locking. Every message is processed immediately in its own handler invocation. Use this for stateless handlers where thread ordering doesn't matter.
Configuration
To tune queue size, eviction, and timing, pass a ConcurrencyConfig object instead of a strategy string:
All options
| Option | Strategies | Default | Description |
|---|---|---|---|
strategy | all | "drop" | The concurrency strategy to use |
maxQueueSize | queue, burst | 10 | Maximum queued messages per thread |
onQueueFull | queue, burst | "drop-oldest" | Whether to evict the oldest or reject the newest message when the queue is full |
queueEntryTtlMs | queue, debounce, burst | 90000 | TTL for queued entries in milliseconds. Expired entries are discarded on dequeue |
debounceMs | debounce, burst | 1500 | Debounce window in milliseconds |
maxLockLifetimeMs | drop, queue, debounce, burst | 600000 | Max total time one handler run may keep renewing its thread lock. After this, renewal stops and the lock lapses at its TTL, so a hung handler cannot block the thread forever |
maxConcurrent | concurrent | Infinity | Max concurrent handlers per thread |
maxConcurrent only applies to the concurrent strategy. Pairing it with any other strategy logs a warning and the value is ignored. Setting maxConcurrent to a value less than 1 throws at construction time, because 0 would deadlock the strategy.
MessageContext
All handler types (onNewMention, onSubscribedMessage, onNewMessage) accept an optional MessageContext as their last parameter. It is populated when using the queue strategy for queued messages, and when using burst for a collapsed turn. A lone burst message receives skipped: [] and totalSinceLastHandler: 1.
The parameter is optional, so existing handlers that don't use context are unaffected.
Example: Pass all messages to an LLM
Lock scope
By default, locks are scoped to the thread, so messages in different threads are processed independently. For platforms like WhatsApp and Telegram where conversations happen at the channel level rather than in threads, the lock scope defaults to "channel".
You can override this globally:
Or resolve it dynamically per message:
State adapter requirements
The queue, debounce, and burst strategies require three additional methods on your state adapter:
| Method | Description |
|---|---|
enqueue(threadId, entry, maxSize) | Atomically append a message to the thread's queue. Returns new depth. |
dequeue(threadId) | Pop the next (oldest) message from the queue. Returns null if empty. |
queueDepth(threadId) | Return the current number of queued messages. |
All built-in state adapters (@chat-adapter/state-memory, @chat-adapter/state-redis, @chat-adapter/state-ioredis) implement these methods. The Redis adapters use Lua scripts for atomicity.
An adapter can namespace these state keys by tenant, so the key passed to your state adapter isn't always the bare thread ID. The Slack adapter in multi-workspace mode scopes deduplication, locks, queues, concurrency slots, and turn cancellation by installation. If you inspect state or recover from an incident with calls like queueDepth(threadId) or forceReleaseLock(threadId), those calls won't reach the namespaced keys. Log events still report the public threadId.
Observability
All strategies emit structured log events at info level:
| Event | Strategy | Data |
|---|---|---|
message-queued | queue, burst | threadId, messageId, queueDepth |
message-dequeued | queue, debounce, burst | threadId, messageId, skippedCount for queue/burst |
message-dropped | drop, queue, burst | threadId, messageId, reason |
message-expired | queue, debounce, burst | threadId, messageId |
message-superseded | debounce | threadId, droppedId |
message-debouncing | debounce, burst | threadId, messageId, debounceMs |
message-debounce-reset | debounce | threadId, messageId |
Choosing a strategy
| Use case | Strategy | Why |
|---|---|---|
| Simple bots, one-shot commands | drop | No queue to configure and no queue overhead |
| AI chatbots, customer support | queue | Messages that arrive mid-run are queued, up to maxQueueSize, and the handler sees them in context.skipped |
| AI chatbots with multi-message user turns | burst | Wait for the idle burst window, then respond once with every message in that window |
| WhatsApp/Telegram bots, rapid corrections | debounce | Users send many short messages in quick succession; wait briefly and keep only the latest |
| Stateless lookups, translations | concurrent | Highest throughput when ordering doesn't matter |
Backward compatibility
- The default strategy is
drop, so existing behavior is unchanged. - The deprecated
onLockConflictoption continues to work but should be replaced withconcurrency. - Handler signatures are backward-compatible; the new
contextparameter is optional. - Deduplication always runs regardless of strategy.
Read more
State Adapters
State adapters store thread subscriptions, distributed locks, and cached data for your bot.
Streaming
Stream real-time text responses from AI models and other async sources to chat platforms.
Error Handling
Handle rate limits, unsupported features, and other errors from adapters.
Handling Events
Register handlers for mentions, messages, reactions, member joins, and platform-specific events.