LLM Request System Prompt Extractor (inspector/contextKey.ts)

LLM-Router · src/mitm/inspector/contextKey.ts · typescript · MIT
ai, dev-tools, lang:typescript

Extracts system prompt strings from various LLM provider API request bodies. Reuse when inspecting or routing intercepted AI requests.

// Source: https://github.com/cmahmud/LLM-Router | License: MIT | Copyright 2026 diegosouzapw
// Provided via justclaw.it.com; keep this notice with the code.
import { createHash } from "node:crypto";
import type { InterceptedRequest } from "./types";

/**
 * Extract the system prompt string from an intercepted LLM request body.
 * Supports OpenAI/Anthropic chat (messages[0] role=system),
 * Anthropic messages API (top-level `system` field),
 * and Gemini (systemInstruction.parts[].text).
 *
 * @returns Concatenated system prompt string, or null if not found.
 */
export function extractSystemPrompt(req: InterceptedRequest): string | null {
  if (!req.requestBody) return null;

  let parsed: unknown;
  try {
    parsed = JSON.parse(req.requestBody);
  } catch {
    return null;
  }

  if (!

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

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

Get a free API key