Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Give the LangGraph Bot's model one system prompt on Anthropic and Gemini (#599)

kevindb068b99aa14
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c0db3aba..fc6dd01e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,15 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### The LangGraph Bot answers on Anthropic and Gemini
+
+With `BOT_PROVIDER=anthropic` or `BOT_PROVIDER=google`, the LangGraph Bot answered nothing: every run
+ended in an error before the model was asked. Those two providers take one system prompt, at the top,
+and the Bot handed its model several: its own guidance, the context the app sends, and the
+coworker's standing role, which the server puts at the head of every run. The provider's LangChain
+integration refused the second one. On those two providers the Bot now folds them into one system
+prompt, in the same order, and a skill picked for a message joins it. Runs on OpenAI are unchanged.
+
### The proof-of-concept Bot reads a model name set with whitespace around it
A `BOT_MODEL` carrying a leading space reached this Bot as it was written. Its startup check refuses
diff --git a/agent-langgraph/src/history.ts b/agent-langgraph/src/history.ts
index 4e124df9..ec575db5 100644
--- a/agent-langgraph/src/history.ts
+++ b/agent-langgraph/src/history.ts
@@ -22,8 +22,22 @@ import { userContent } from "../../shared/user-content";
*/
export { NO_ANSWER_CAME };
+/**
+ * The providers that take one system prompt, and take it first.
+ *
+ * Anthropic's Messages API has the system prompt as a field of the request rather than as a turn,
+ * and Gemini has it as `systemInstruction`, so neither integration has anywhere to put a second one:
+ * `@langchain/anthropic` throws "System messages are only permitted as the first passed message."
+ * and `@langchain/google-genai` throws "System message should be the first one", both before any
+ * request is made. OpenAI takes a system turn anywhere in a conversation.
+ */
+const ONE_SYSTEM_PROMPT = new Set(["anthropic", "google"]);
+
/** Translate the conversation AG-UI carries into LangChain's message classes. */
-export function toLangChainMessages(input: RunAgentInput): BaseMessage[] {
+export function toLangChainMessages(
+ input: RunAgentInput,
+ provider = "openai",
+): BaseMessage[] {
const messages: BaseMessage[] = [
new SystemMessage(COMPUTER_GUIDANCE),
// AG-UI carries application context separately from conversation history. CopilotKit puts
@@ -134,12 +148,37 @@ export function toLangChainMessages(input: RunAgentInput): BaseMessage[] {
messages.push(new HumanMessage(CONTINUE_TURN));
}
- return messages;
+ /*
+ * Every run holds more than one system message, so on those providers every run failed.
+ *
+ * The computer guidance opens this list and the caller's context follows it, and the server puts a
+ * coworker's standing role at the head of `input.messages` on every run it sends a remote Bot. A
+ * skill somebody picks arrives as a system turn ahead of their message, too. So a Bot set to
+ * `BOT_PROVIDER=anthropic` or `google` answered nothing at all: each run ended in the integration's
+ * refusal, before the model was asked.
+ *
+ * Folded into one, in the order given, and only for those providers. On OpenAI a skill's
+ * instruction stays beside the message it was picked for.
+ */
+ return ONE_SYSTEM_PROMPT.has(provider)
+ ? withOneSystemPrompt(messages)
+ : messages;
}
/** The continuation a strict provider needs when a run carries only deltas. See toLangChainMessages. */
const CONTINUE_TURN = "Continue from where the conversation above left off.";
+/** Every system message's text as one system message at the top, and the rest as they were. */
+function withOneSystemPrompt(messages: BaseMessage[]): BaseMessage[] {
+ const system = messages.filter((message) => message instanceof SystemMessage);
+ return [
+ new SystemMessage(
+ system.map((message) => String(message.content)).join("\n\n"),
+ ),
+ ...messages.filter((message) => !(message instanceof SystemMessage)),
+ ];
+}
+
function parseArguments(raw: string): Record<string, unknown> {
try {
const parsed = JSON.parse(raw || "{}");
diff --git a/agent-langgraph/src/index.ts b/agent-langgraph/src/index.ts
index a4f3fef9..7fb7d765 100644
--- a/agent-langgraph/src/index.ts
+++ b/agent-langgraph/src/index.ts
@@ -407,7 +407,7 @@ async function runAgent(input: RunAgentInput): Promise<Response> {
await streamRun(
async () =>
buildGraph(input).streamEvents(
- { messages: toLangChainMessages(input) },
+ { messages: toLangChainMessages(input, PROVIDER) },
{ version: "v2" },
),
input,
diff --git a/agent-langgraph/tests/history.test.ts b/agent-langgraph/tests/history.test.ts
index 0e8fe764..1d81668c 100644
--- a/agent-langgraph/tests/history.test.ts
+++ b/agent-langgraph/tests/history.test.ts
@@ -1,10 +1,13 @@
import { describe, expect, test } from "bun:test";
import type { RunAgentInput } from "@ag-ui/core";
+import { ChatAnthropic } from "@langchain/anthropic";
import {
AIMessage,
SystemMessage,
ToolMessage,
} from "@langchain/core/messages";
+import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
+import { COMPUTER_GUIDANCE } from "../../shared/bot-prompt";
import { NO_ANSWER_CAME, toLangChainMessages } from "../src/history";
/**
@@ -198,3 +201,146 @@ describe("a message with a file attached", () => {
expect(userContent("What is 17 times 3?")).toBe("What is 17 times 3?");
});
});
+
+/**
+ * A run as the server sends it to a remote Bot: the coworker's standing role at the head of the
+ * messages, the caller's context beside them, and a skill somebody picked as a system turn just ahead
+ * of the message it was picked for.
+ *
+ * Anthropic and Gemini take one system prompt, at the top, and their LangChain integrations refuse a
+ * second before any request is made. With the computer guidance this module puts first, every run
+ * the server sends holds at least two.
+ */
+describe("a provider that takes one system prompt", () => {
+ const run = () => {
+ const shaped = input([
+ {
+ id: "standing-role:bot_1",
+ role: "system",
+ content: "You are Ada, Analyst.",
+ },
+ { id: "u1", role: "user", content: "Summarise the Q3 filing." },
+ { id: "a1", role: "assistant", content: "Revenue rose 4%." },
+ { id: "s1", role: "system", content: "Answer in bullet points." },
+ { id: "u2", role: "user", content: "Again, shorter." },
+ ]);
+ shaped.context = [{ description: "A2UI Component Schema", value: "{}" }];
+ return shaped;
+ };
+ // In the order the model is given them: the context beside the run, then the turns.
+ const instructions = [
+ "A2UI Component Schema\n{}",
+ "You are Ada, Analyst.",
+ "Answer in bullet points.",
+ ];
+
+ test("folds every system message into one at the top, in order", () => {
+ for (const provider of ["anthropic", "google"]) {
+ const messages = toLangChainMessages(run(), provider);
+
+ const system = messages.filter((m) => m instanceof SystemMessage);
+ expect(system).toHaveLength(1);
+ expect(messages[0]).toBe(system[0] as SystemMessage);
+ const prompt = String(system[0]?.content);
+ expect(prompt.startsWith(COMPUTER_GUIDANCE)).toBe(true);
+ const at = instructions.map((text) => prompt.indexOf(text));
+ expect(at.every((index) => index > 0)).toBe(true);
+ expect(at).toEqual([...at].sort((a, b) => a - b));
+ expect(messages.slice(1).map((m) => String(m.content))).toEqual([
+ "Summarise the Q3 filing.",
+ "Revenue rose 4%.",
+ "Again, shorter.",
+ ]);
+ }
+ });
+
+ test("is accepted by Anthropic", async () => {
+ let sent: { system?: unknown; messages?: { role: string }[] } = {};
+ const model = new ChatAnthropic({
+ model: "claude-sonnet-4-5",
+ apiKey: "sk-ant-test",
+ clientOptions: {
+ fetch: (async (_url: unknown, init?: RequestInit) => {
+ sent = JSON.parse(String(init?.body));
+ return Response.json({
+ id: "msg_1",
+ type: "message",
+ role: "assistant",
+ model: "claude-sonnet-4-5",
+ content: [{ type: "text", text: "- Revenue rose 4%." }],
+ stop_reason: "end_turn",
+ stop_sequence: null,
+ usage: { input_tokens: 1, output_tokens: 1 },
+ });
+ }) as typeof fetch,
+ },
+ });
+
+ const answer = await model.invoke(toLangChainMessages(run(), "anthropic"));
+
+ expect(answer.content).toBe("- Revenue rose 4%.");
+ for (const text of instructions) {
+ expect(JSON.stringify(sent.system)).toContain(
+ JSON.stringify(text).slice(1, -1),
+ );
+ }
+ expect(sent.messages?.map((m) => m.role)).toEqual([
+ "user",
+ "assistant",
+ "user",
+ ]);
+ });
+
+ test("is accepted by Gemini", async () => {
+ let sent: {
+ systemInstruction?: unknown;
+ contents?: { role: string }[];
+ } = {};
+ const realFetch = globalThis.fetch;
+ globalThis.fetch = (async (_url: unknown, init?: RequestInit) => {
+ sent = JSON.parse(String(init?.body));
+ return Response.json({
+ candidates: [
+ {
+ content: { role: "model", parts: [{ text: "- Revenue rose 4%." }] },
+ finishReason: "STOP",
+ index: 0,
+ },
+ ],
+ });
+ }) as typeof fetch;
+ try {
+ const model = new ChatGoogleGenerativeAI({
+ model: "gemini-2.5-flash",
+ apiKey: "test",
+ });
+
+ const answer = await model.invoke(toLangChainMessages(run(), "google"));
+
+ expect(answer.content).toBe("- Revenue rose 4%.");
+ } finally {
+ globalThis.fetch = realFetch;
+ }
+ for (const text of instructions) {
+ expect(JSON.stringify(sent.systemInstruction)).toContain(
+ JSON.stringify(text).slice(1, -1),
+ );
+ }
+ expect(sent.contents?.map((c) => c.role)).toEqual([
+ "user",
+ "model",
+ "user",
+ ]);
+ });
+
+ test("leaves an OpenAI conversation's system turns where they were", () => {
+ // OpenAI takes a system turn anywhere, so a skill stays beside the message it was picked for.
+ const messages = toLangChainMessages(run(), "openai");
+ expect(
+ messages
+ .map((m) => (m instanceof SystemMessage ? "system" : String(m.content)))
+ .slice(-3),
+ ).toEqual(["Revenue rose 4%.", "system", "Again, shorter."]);
+ expect(messages.filter((m) => m instanceof SystemMessage)).toHaveLength(4);
+ });
+});