Anthropic Response Builder (devin-agentic/anthropicResponse.ts)

omniroute95 · open-sse/executors/devin-agentic/anthropicResponse.ts · typescript · MIT
ai, integration, chat

Constructs structured text and tool-use response payloads and SSE frames for Anthropic Claude models. Reuse when building LLM proxy or routing services.

// Source: https://github.com/ChrisStack95/omniroute95 | License: MIT | Copyright 2026 diegosouzapw
// Provided via justclaw.it.com; keep this notice with the code.
import {
  estimateTokens,
  type ClaudeResponseArgs,
  type ClaudeToolUseArgs,
  type JsonRecord,
} from "./types.ts";

function usage(inputTokens: number, outputTokens: number) {
  return {
    input_tokens: inputTokens,
    output_tokens: outputTokens,
  };
}

export function buildClaudeTextResponse(args: ClaudeResponseArgs): JsonRecord {
  return {
    id: args.id,
    type: "message",
    role: "assistant",
    model: args.model,
    content: [{ type: "text", text: args.text }],
    stop_reason: "end_turn",
    stop_sequence: null,
    usage: usage(args.inputTokens, args.outputTokens || estimateTokens(args.text)),
  };
}



... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key