Skip to content

tool results not persisted to chat history via streamText() #108

@ericpaulsen

Description

@ericpaulsen

Describe the bug

Tool results are not being persisted to chat history when streamText() returns with tool calls, causing Anthropic API errors on subsequent handler loops. The chat history becomes malformed (assistant messages with tool_use blocks not followed by user messages with tool_result blocks).

Initial trigger: The corruption appears to have originated from Lambda timeouts/crashes interrupting tool result recording. When the Lambda crashed mid-tool-execution:

  1. Tool may have executed (e.g., Slack message posted)
  2. Tool result was never recorded to chat history
  3. Chat left in inconsistent state
  4. Subsequent requests to that chat failed with malformed history

Observed errors during crashes:

Runtime.UnhandledPromiseRejection: timeout
Runtime.ExitError
LAMBDA_RUNTIME Failed to post handler success response. Http response code: 403

Subsequent Anthropic errors:

messages.1: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_01NkJjQdLn3bRzcxHfDGQz8h

Ongoing issue: After the initial crash-induced corruption, even NEW chats (fresh threads with different chat keys) exhibited similar behavior - tool results not being recorded properly.

Evidence from logs in a fresh chat:

🔍 [Sanitize] Only 1 messages, keeping all    ← User's initial message
📤 [Slack Tool] sendMessage called...
📤 [Slack Tool] sendMessage succeeded         ← Tool executes correctly
Finish reason: tool-calls
[Handler loops]
🔍 [Sanitize] Only 2 messages, keeping all    ← Should be 3 messages (missing tool result)

Expected behavior:

  • Tool results should be recorded as user messages after tool execution
  • If Lambda crashes mid-execution, chat history should either be rolled back or gracefully handle the incomplete state on next request

Environment

  • OS: macOS 25.0.0 (darwin)
  • Node.js version: v22.x (Lambda runtime)
  • Blink version: Latest as of Dec 16, 2025
  • Installation method: bun

Additional Context

Code pattern:

agent.on("chat", async ({ messages, chat }) => {
  const slackTools = slack.createTools({ client: app.client });
  
  return streamText({
    model: anthropic("claude-sonnet-4-5"),
    messages: convertToModelMessages(messages, { ignoreIncompleteToolCalls: true }),
    tools: {
      ...slackTools,
      // other tools...
    },
  });
});

Packages used:

  • blink
  • @blink-sdk/slack
  • ai (Vercel AI SDK v5)
  • @ai-sdk/anthropic

Questions:

  1. Is agent.chat supposed to automatically record tool results when streamText returns with tool calls?
  2. Should Blink handle Lambda crashes/timeouts more gracefully to prevent chat corruption?
  3. Is there a way to repair/reset a corrupted chat history?
  4. Why do new chats (different chat keys) also exhibit tool result recording issues?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions