Skip to content

fix(slack): thread history redundantly included on every message causes token bloat #32121

@sourman

Description

@sourman

Summary

Slack thread sessions include full thread history on every message in a thread, not just the first. This causes unnecessary token bloat and increased API costs - the session transcript already contains the full conversation, so re-fetching and re-injecting thread history on each turn is redundant.

Steps to Reproduce

  1. Start a Slack thread with the bot (e.g., @mention in a channel)
  2. Send multiple follow-up replies in the thread
  3. Check the session file in ~/.openclaw/agents/main/sessions/*.jsonl
  4. Observe [Thread history - for context] wrapper on every message, growing larger each time

Expected Behavior

  • First message in thread session: Include [Thread history - for context] with full thread history
  • Subsequent messages: Rely on session transcript (already has full context), skip redundant thread history fetch

Actual Behavior

Thread history is fetched and included on every single message in a thread session, causing exponential token growth:

Session Evidence (2026-03-02)

Message 2 (user: "checking in"):

[Thread history - for context]
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:45 EST] <@U0AF3PBGARJ> ping
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] Pong. What do you need?
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] :warning: :email: Message: `with message 1772480714.721979, emoji 👍` failed

Message 3 (user: "what is the fifth letter in four"):

[Thread history - for context]
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:45 EST] <@U0AF3PBGARJ> ping
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] Pong. What do you need?
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] :warning: :email: Message: `with message 1772480714.721979, emoji 👍` failed
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:46 EST] checking in
[Slack comply (assistant) Mon 2026-03-02 14:46 EST] All clear here. No pending items, no anomalies. Anything you need?

Message 4 (user: "are you sure"):

[Thread history - for context]
[... entire conversation history repeated again ...]
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:46 EST] what is the fifth letter in four
[Slack comply (assistant) Mon 2026-03-02 14:46 EST] "Four" has 4 letters. No fifth letter exists.

Input token counts showing bloat:

  • Message 1 (ping): 12,379 tokens
  • Message 2: 3,533 tokens
  • Message 3: 4,161 tokens
  • Message 4: 4,892 tokens

The session transcript already contains this full context - re-injecting it on every message is wasteful.

Root Cause

In src/slack/monitor/message-handler/prepare.ts, the resolveSlackThreadHistory function fetches thread history unconditionally. The ThreadStarterBody is then injected on every message via [Thread history - for context] wrapper.

Related

Environment

  • OpenClaw: main (2026-03-02)
  • Channel: Slack (socket mode)
  • Config: channels.slack.thread.historyScope: "thread"

Impact

  • Cost: Unnecessary API calls to fetch thread history on every message
  • Latency: Additional Slack API round-trips per message
  • Tokens: Redundant context inclusion increases prompt size

Suggested Fix

Only fetch and inject thread history for isNewSession (first message in thread session). Subsequent messages should rely on the session transcript which already contains the full conversation history.

Condition should be:

const shouldFetchHistory = !threadSessionPreviousTimestamp;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions