Skip to content

feat(agent-service): use the AI SDK's native message history - #9494

Open
bobbai00 wants to merge 4 commits into
apache:mainfrom
bobbai00:feat/agent-native-messages
Open

bobbai00 wants to merge 4 commits into
apache:mainfrom
bobbai00:feat/agent-native-messages

Conversation

@bobbai00

@bobbai00 bobbai00 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

Before every model call, the agent replaced the AI SDK's messages with one summary message built around the workflow. This PR sends the SDK's own messages instead: the system prompt, then the user's messages, the assistant's messages and tool calls, and the tool results. The service adds only a short note when a turn was stopped or failed ("Generation stopped by user." or "Error: "), and an error result for a tool call the SDK did not carry out. The model reads the workflow through two new tools, get_workflow and get_operator_result. Two things follow:

  1. Any tool's result reaches the model as it is. On main the summary shows only the workflow in full and cuts any other tool's successful result to its first line, so a tool about anything else needs its own section in the summary code, as feat(agent-service): add docs search and read tools #9424 adds one for its docs tools. Tools for datasets and the platform's other resources, and tools from MCP servers (Serve the agent's workflow tools from a Texera MCP server #9457), need no change to the context code here. The workflow itself is read through a tool, like any other resource.
  2. Prompt caching reuses the conversation. LiteLLM's automatic caching (enable_anthropic_prompt_caching, which feat(deploy): upgrade LiteLLM and turn on prompt caching #9589 turns on, with no cache points set by hand) marks the system prompt and the last message, and the next request reuses what it repeats exactly. On main the last message is the summary, rewritten before every call, so only the tool definitions and system prompt are reused. Here every call begins with the previous call's input.
main This PR
What the model reads One summary message (conversation, then dataflow), rewritten before every call The conversation as it happened: user and assistant messages, tool calls and tool results
A past tool call Name, summary, first line of the result (a failed call's whole error) Arguments and the whole result
Adding a tool Its result must fit on one line, or the summary needs a new section No change to the context code
The workflow Inside the summary get_workflow: each operator's properties, schemas and compilation error, and whether its result is there, outdated or failed; then the links
An operator's result Inside the summary In the tool result of the call that executed it; get_operator_result returns it again, or says it is outdated
Executing a changed operator After the step, by the service; result only in the summary Inside the add or modify call; result in the tool result
Reused from the prompt cache The tool definitions and system prompt Those and the earlier conversation
Loop generateText with onStepFinish (deprecated in AI SDK 7) ToolLoopAgent with onStepEnd
System prompt Describes the summary's format (1.4 KB) Describes what get_workflow returns

disabledTools cannot switch the two read tools off, since nothing else shows the model the workflow. In the agent panel, the step details show the conversation as the model receives it, and the Tools tab lists the two new tools.

Any related issues, documentation, discussions?

Closes #9455
Part of #9453

How was this PR tested?

  • cd agent-service && bun test (365 pass), bun run typecheck, bun run format:check. With the SDK's mock model, the tests pin the exact input of each model call (native messages only, each call beginning with the previous call's input), what get_workflow and get_operator_result return, and when a result counts as outdated.

  • 20 tasks from KramaBench, a benchmark of data-science questions over data files, drawn at random from the 59 of its 104 tasks whose input files are all CSVs and whose answers are scored without an LLM judge, main against this PR on a single-node stack. Claude Haiku 5.5 through LiteLLM 1.110.2 (feat(deploy): upgrade LiteLLM and turn on prompt caching #9589 pins 1.108.17, whose automatic caching marks the same two points) with enable_anthropic_prompt_caching: true and no cache points configured, one attempt per task and side, at most 40 steps. The tasks, prompt and scoring are those of the fork bobbai00/Kramabench, with each task's files uploaded as a dataset and named in the prompt by their dataset paths.

    20 KramaBench tasks, all together main This PR
    Correct answers 12 12, the same tasks
    Input tokens sent 1.57M 2.02M (+29%)
    Input read from cache 82% 96%
    Input cost $0.0487 $0.0293 (−40%)
    Total cost, output included $0.0912 $0.0680 (−25%)
    Model calls 109 139

    Although this PR sends more tokens, it costs less: every call began with the previous call's input (119 of 119), so the cache could reuse it, while no call on main did (0 of 89), because its summary changes on every call. The costs use Claude Haiku 5.5's list prices.

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5 and Claude Fable 5.2)

@bobbai00

Copy link
Copy Markdown
Contributor Author

The evaluation from #9495 on this branch, against main, with Claude Haiku 5.5 and three runs per case:

main This PR
Runs passed 9 of 18 9 of 18
Model calls per run 2.5 2.4
Input tokens per run 32,506 31,180
Output tokens per run 964 721
KB per call 37.6 36.9

The same cases pass and fail on both: the docs question, a change the user makes between two prompts, and the user's own execution are what later sub-issues of #9453 address. Most of each call is still the system prompt, which #9456 and #9481 shrink.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Automated Reviewer Suggestions

Based on the git blame history of the changed files, we recommend the following reviewers:

  • Contributors with relevant context: @aglinxinyuan, @oliarmak-web, @ShawnGarello
    You can notify them by mentioning @aglinxinyuan, @oliarmak-web, @ShawnGarello in a comment.

@codecov-commenter

codecov-commenter commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.68%. Comparing base (a1b5aad) to head (bc92e98).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9494      +/-   ##
============================================
- Coverage     91.68%   91.68%   -0.01%     
  Complexity     5125     5125              
============================================
  Files          1305     1305              
  Lines         58211    58205       -6     
  Branches       7594     7594              
============================================
- Hits          53371    53365       -6     
  Misses         2933     2933              
  Partials       1907     1907              
Flag Coverage Δ *Carryforward flag
access-control-service 77.44% <ø> (ø) Carriedforward from a1b5aad
agent-service 99.56% <100.00%> (-0.01%) ⬇️
amber 85.79% <ø> (ø) Carriedforward from a1b5aad
computing-unit-managing-service 63.83% <ø> (ø) Carriedforward from a1b5aad
config-service 87.50% <ø> (ø) Carriedforward from a1b5aad
file-service 82.16% <ø> (ø) Carriedforward from a1b5aad
frontend 96.43% <ø> (ø) Carriedforward from a1b5aad
notebook-migration-service 83.77% <ø> (ø) Carriedforward from a1b5aad
pyamber 98.32% <ø> (ø) Carriedforward from a1b5aad
result-service 86.48% <ø> (ø) Carriedforward from a1b5aad
workflow-compiling-service 80.90% <ø> (ø) Carriedforward from a1b5aad

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bobbai00
bobbai00 force-pushed the feat/agent-native-messages branch from 9ff9816 to bea6728 Compare November 19, 2026 08:38
@bobbai00

Copy link
Copy Markdown
Contributor Author

@yrenat @sshiv012 Can you guys review this PR ? I feel that both of you have first-hand experiences with developing TexeraAgent

@sshiv012 sshiv012 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserving native messages makes sense, but I’m not yet convinced the practical benefit is demonstrated here. The evaluation reports the same 9/18 passing runs, with roughly 4% fewer input tokens and 2% smaller payloads.

Keeping full tool arguments and results also means retaining previous operator code and execution outputs alongside the current dataflow.

Could you add
a. A realistic case where the information lost by the current summary causes a failure that this PR resolves.
b. A longer conversation with repeated edits and executions, comparing correctness, token usage, and payload growth.

This would help establish whether the additional history improves the agent enough to justify its cost. If this is primarily groundwork for later changes, please identify which upcoming requirement depends on it.

Comment thread agent-service/src/agent/texera-agent.ts Outdated
*/

import { generateText, type ModelMessage, type LanguageModel, stepCountIs } from "ai";
import { ToolLoopAgent, isStepCount, type ModelMessage, type LanguageModel } from "ai";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For durability reasons, can we consider evaluating WorkflowAgent before adopting ToolLoopAgent (in-memory) here? WorkflowAgent provides durable execution through the Workflow runtime, which also supports OpenTelemetry integration with our observability layer. Could you explain the intended durability architecture and why ToolLoopAgent is the appropriate choice?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on durability. WorkflowAgent runs the same tool loop on the Workflow runtime, so it needs that runtime and a store, and the agent's conversation and workflow copy would also have to be saved for a restart to pick them up. That needs its own design, so I'd like to leave it out of this PR and stay on ToolLoopAgent for now.

bobbai00 and others added 4 commits November 23, 2026 22:56
Before every model call, the agent dropped the SDK's messages and wrote one
user message: a summary of the conversation followed by the current
dataflow. A past tool call appeared as its name, its summary and the first
line of its result.

Each step now keeps the model messages it added: the user's message, or the
assistant message with its tool calls and the tool results. The model reads
these along the path to HEAD, so a branch HEAD has left stays out of sight.
The current dataflow is one message at the end of every step's input,
written again each time and never kept. The loop runs as the SDK's
ToolLoopAgent, and the system prompt describes only the dataflow block.

A tool call the SDK did not run, because its response was cut off, is kept
with a failed result: the SDK refuses a conversation in which a call has no
result, so every later turn would fail.
After addOperator or modifyOperator the service executed the operator
itself, outside the tool call, and the result reached the model only
through the dataflow block. That block is not kept, so on a later turn the
agent found numbers in its own answers that no tool result showed, and told
the user they were unverified.

The two tools now execute the operator they changed and return the
execution's result after their own line. A failed or stopped execution
follows the change and does not turn it into a failure. The result is
still filed under the step that made the change.
Every model call ended with a dataflow block that the service rebuilt for
that call, so the last message changed on every call. A prompt cache
point on the last message, which LiteLLM's automatic caching places, was
never read back, and only the tool definitions and system prompt were
reused.

The block is gone: the model reads the SDK's own messages, so every call
begins with the previous call's input. It reads the workflow with two new
tools. getWorkflow lists each operator with its properties, schemas,
compilation error and whether its result is there, outdated or failed,
then the links. getOperatorResult returns an operator's latest result
without executing anything, or says that it is outdated. Each result now
records what it was computed from, so a change to the operator or to one
it reads from makes the result outdated. disabledTools cannot switch the
two tools off. The result formatter that only fed the block is removed.
getWorkflow and getOperatorResult become get_workflow and
get_operator_result. New tools are named in snake_case; the four
existing ones keep their names.
@bobbai00
bobbai00 force-pushed the feat/agent-native-messages branch from 8944c07 to bc92e98 Compare November 24, 2026 06:56

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use the AI SDK's native message history in the agent

4 participants