Skip to content

Avoid duplicate prompt message events in LangChain callback #5

Description

@daugaard

Problem

The LangChain callback currently emits standalone message events for every system/user message seen in on_chat_model_start.

This creates duplicate telemetry because the same prompt messages are already stored inside the corresponding llm event input under input.messages.

In a tool-using interaction, this becomes especially noisy:

system_message
user_message
user_message
user_message
user_message
llm assistant_response
retrieval
tool
system_message
user_message
user_message
user_message
user_message
llm assistant_response

Conceptually, those repeated system/user messages are not new events in the interaction. They are the prompt frame for each model call.

This makes SessionBat's interaction view harder to understand and stores redundant data.

Proposed Change

Change the LangChain callback so it does not auto-emit standalone prompt message events from on_chat_model_start by default.

Instead, the default LangChain telemetry should record:

  • one llm event per model call, including input.messages
  • one tool event per tool call
  • one retrieval event per retriever call
  • no separate message events derived from chat model prompt input

Manual message APIs should continue to work:

interaction.user_message("...")
interaction.message(role="user", content="...")

Those are explicit app-level observations and should not be affected.

Suggested API

Add an opt-in flag for users who still want prompt messages emitted as standalone events:

handler = client.langchain_callback(record_prompt_messages=True)

Default:

record_prompt_messages=False

Implementation Notes

Current duplication appears to come from on_chat_model_start calling _record_messages_from_chat_input(...).

Desired behavior:

  • keep storing full prompt messages in the llm event request/input
  • skip _record_messages_from_chat_input(...) unless record_prompt_messages=True
  • update tests that currently expect auto-created system_message / user_message events from LangChain chat model input

Why Not Remove Retrieval Or Tool Events?

Do not remove either retrieval or tool events for now.

Even when they look duplicated in the UI, they represent different diagnostic layers:

  • retrieval: documents returned from the retriever/vector/index layer
  • tool: the tool result returned back to the model

The SessionBat app should group those into one visual execution step when appropriate, but the SDK should preserve both raw observations.

Acceptance Criteria

  • LangChain chat model invocations no longer create standalone message events by default.
  • The corresponding llm event still includes input.messages.
  • record_prompt_messages=True restores the current standalone message-event behavior.
  • Manual interaction.user_message(...) and interaction.message(...) APIs are unchanged.
  • Tool and retriever event behavior is unchanged.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions