anthropic-thinking-disable.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { streamSimple } from "../src/stream.ts";
  4. import type { Context, Model, SimpleStreamOptions } from "../src/types.ts";
  5. interface AnthropicThinkingPayload {
  6. thinking?: { type: string; budget_tokens?: number; display?: string };
  7. output_config?: { effort?: string };
  8. }
  9. class PayloadCaptured extends Error {
  10. constructor() {
  11. super("payload captured");
  12. this.name = "PayloadCaptured";
  13. }
  14. }
  15. function makePayloadCaptureContext(): Context {
  16. return {
  17. messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
  18. };
  19. }
  20. async function capturePayload(
  21. model: Model<"anthropic-messages">,
  22. options?: SimpleStreamOptions,
  23. ): Promise<AnthropicThinkingPayload> {
  24. let capturedPayload: AnthropicThinkingPayload | undefined;
  25. const payloadCaptureModel: Model<"anthropic-messages"> = {
  26. ...model,
  27. baseUrl: "http://127.0.0.1:9",
  28. };
  29. const s = streamSimple(payloadCaptureModel, makePayloadCaptureContext(), {
  30. ...options,
  31. apiKey: "fake-key",
  32. onPayload: (payload) => {
  33. capturedPayload = payload as AnthropicThinkingPayload;
  34. throw new PayloadCaptured();
  35. },
  36. });
  37. await s.result();
  38. if (!capturedPayload) {
  39. throw new Error("Expected payload to be captured before request failure");
  40. }
  41. return capturedPayload;
  42. }
  43. interface RunResult {
  44. thinkingEventCount: number;
  45. thinkingCharCount: number;
  46. text: string;
  47. contentTypes: string[];
  48. }
  49. function makeE2EContext(): Context {
  50. return {
  51. systemPrompt: "You are a precise assistant. Follow the requested output format exactly.",
  52. messages: [
  53. {
  54. role: "user",
  55. content:
  56. "Before replying, carefully solve 36863 * 5279 internally. Then reply with the word pong repeated exactly 40 times, separated by single spaces. Do not add any other text.",
  57. timestamp: Date.now(),
  58. },
  59. ],
  60. };
  61. }
  62. function countPongs(text: string): number {
  63. return text.match(/\bpong\b/gi)?.length ?? 0;
  64. }
  65. async function runWithoutReasoning(model: Model<"anthropic-messages">): Promise<RunResult> {
  66. const s = streamSimple(model, makeE2EContext(), {
  67. temperature: 0,
  68. maxTokens: 160,
  69. });
  70. let thinkingEventCount = 0;
  71. let thinkingCharCount = 0;
  72. for await (const event of s) {
  73. if (event.type === "thinking_start" || event.type === "thinking_end") {
  74. thinkingEventCount += 1;
  75. }
  76. if (event.type === "thinking_delta") {
  77. thinkingEventCount += 1;
  78. thinkingCharCount += event.delta.length;
  79. }
  80. }
  81. const response = await s.result();
  82. expect(response.stopReason, response.errorMessage).toBe("stop");
  83. const text = response.content
  84. .filter((block) => block.type === "text")
  85. .map((block) => block.text)
  86. .join("")
  87. .trim();
  88. return {
  89. thinkingEventCount,
  90. thinkingCharCount,
  91. text,
  92. contentTypes: response.content.map((block) => block.type),
  93. };
  94. }
  95. describe("Anthropic thinking disable payload", () => {
  96. it("sends thinking.type=disabled for budget-based reasoning models when thinking is off", async () => {
  97. const payload = await capturePayload(getModel("anthropic", "claude-sonnet-4-5"));
  98. expect(payload.thinking).toEqual({ type: "disabled" });
  99. expect(payload.output_config).toBeUndefined();
  100. });
  101. it("sends thinking.type=disabled for adaptive reasoning models when thinking is off", async () => {
  102. const payload = await capturePayload(getModel("anthropic", "claude-opus-4-6"));
  103. expect(payload.thinking).toEqual({ type: "disabled" });
  104. expect(payload.output_config).toBeUndefined();
  105. });
  106. it("sends thinking.type=disabled for Claude Opus 4.8 when thinking is off", async () => {
  107. const payload = await capturePayload(getModel("anthropic", "claude-opus-4-8"));
  108. expect(payload.thinking).toEqual({ type: "disabled" });
  109. expect(payload.output_config).toBeUndefined();
  110. });
  111. it("uses adaptive thinking for Claude Opus 4.8 when reasoning is enabled", async () => {
  112. const payload = await capturePayload(getModel("anthropic", "claude-opus-4-8"), { reasoning: "high" });
  113. expect(payload.thinking).toEqual({ type: "adaptive", display: "summarized" });
  114. expect(payload.output_config).toEqual({ effort: "high" });
  115. });
  116. it("maps xhigh reasoning to effort=xhigh for Claude Opus 4.8", async () => {
  117. const payload = await capturePayload(getModel("anthropic", "claude-opus-4-8"), { reasoning: "xhigh" });
  118. expect(payload.thinking).toEqual({ type: "adaptive", display: "summarized" });
  119. expect(payload.output_config).toEqual({ effort: "xhigh" });
  120. });
  121. });
  122. describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic thinking disable E2E", () => {
  123. it("disables thinking for Claude reasoning models", { retry: 2, timeout: 30000 }, async () => {
  124. const result = await runWithoutReasoning(getModel("anthropic", "claude-sonnet-4-5"));
  125. expect(result.thinkingEventCount).toBe(0);
  126. expect(result.thinkingCharCount).toBe(0);
  127. expect(result.contentTypes).not.toContain("thinking");
  128. expect(countPongs(result.text)).toBeGreaterThanOrEqual(35);
  129. });
  130. });