Skip to content

Improve AI context quality for better LLM reasoning#164

Open
bajman wants to merge 2 commits intodev-chat:masterfrom
bajman:fix/ai-context-quality
Open

Improve AI context quality for better LLM reasoning#164
bajman wants to merge 2 commits intodev-chat:masterfrom
bajman:fix/ai-context-quality

Conversation

@bajman
Copy link

@bajman bajman commented Mar 15, 2026

Summary

  • Resolve user mentions: <@U123ABC> in message history is now resolved to @displayname before being sent to the LLM, so the model can identify conversational participants
  • Add channel context: System prompts now include the channel name (e.g. #politics), allowing the model to calibrate tone and topic relevance
  • Fix prompt structure for /prompt-with-history: Conversation history was incorrectly embedded in system instructions — moved it to user input where the model expects content to reason over, with a proper behavioral system prompt
  • Deduplicate triggering message for @moonbeam: The tagged message was sent to the model twice (from DB history + manual append), wasting tokens
  • Add token-aware history truncation: New truncateToTokenBudget() utility drops oldest messages when history exceeds ~8000 tokens, preventing context overflow
  • Replace hardcoded userIdId != 39: Dynamic lookup of Moonbeam's DB ID instead of hardcoded value that would silently break if the row changed
  • Give /ai/text basic context: The stateless /ai/text path now receives channel name and requesting user's name in its system prompt

Test plan

  • Verify @moonbeam responses reference users by name, not raw Slack IDs
  • Verify /prompt responses are contextually appropriate to the channel topic
  • Verify /ai/text responses acknowledge the channel context
  • Verify @moonbeam doesn't repeat the triggering message in its reasoning
  • Run npx jest in packages/backend — all tests related to changed files pass
  • Verify no regression in existing AI response quality

🤖 Generated with Claude Code

bajman and others added 2 commits March 14, 2026 21:51
LLM responses containing standard markdown (### headers, **bold**,
* bullet lists) were rendering as raw text in Slack because Slack
uses its own mrkdwn format. The existing markdownToSlackMrkdwn()
converter was incomplete and not applied to all code paths.

Resolves this by adopting Slack's new markdown block type, which
natively translates standard markdown server-side. Applied across
all four AI text paths: /ai/text, /ai/prompt-with-history,
@moonbeam, and redeployMoonbeam. Removes the manual converter
and getChunks utility.

Also upgrades @slack/web-api v6 → v7.15.0 with associated
breaking change fixes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
LLM responses were degraded by poor context assembly: user mentions
passed as raw Slack IDs (<@u123abc>) left the model unable to
identify who was being referenced, no channel context meant the
model couldn't calibrate tone for #politics vs #dev, and
/prompt-with-history embedded conversation history in system
instructions rather than user input — violating the model's
expected separation between behavioral guidance and content to
reason over. The @moonbeam path also sent the triggering message
twice (once from the DB, once appended), wasting tokens that
could carry useful conversation history.

Resolves this by resolving mentions to display names so the model
can track conversational participants, injecting channel name and
topic into system prompts for tone calibration, restructuring
prompt-with-history to place history in user input where the model
expects content, deduplicating the triggering message, adding
token-aware history truncation to maximize useful context within
budget, replacing the hardcoded userIdId != 39 exclusion with a
dynamic lookup, and giving /ai/text basic channel and user context.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sfreeman422
Copy link
Collaborator

Will review this PR once #163 is approved + merged in. You have duplicative changes here that will lead to merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants