An org.llm4s.http.Llm4sHttpClient that records a provider's HTTP exchanges to a cassette file and replays them (#459), for testing code that calls a real provider client without a network or an API key.
Pass it to the client as LlmClientOptions.default.withHttpClient(cassette). It reaches every provider that sends through Llm4sHttpClient (openai-compatible and its dialects, Gemini, Vertex AI, Ollama, Perplexity, watsonx); the openai, anthropic and bedrock chat clients send through their vendors' SDKs and ignore it.
'''The cassette''' is one JSON file: "llm4sCassette": 1 and the interactions in the order they happened, each a request (method, url, headers, body) and a response (status, headers, body, and masked: whether recording masked a value of the body; optional, and read as unknown when absent). A body is {"json": ...} (a JSON request body, keys sorted), {"text": "..."} or {"base64": "..."}, and absent when empty. A response body is kept as received, as text or base64, so a streamed (SSE) reply replays byte for byte.
'''What is masked before anything is written''' (to [REDACTED]):
- every value llm4s holds as a
Secret, wherever it appears in the URL, the headers or a body (a body that is not UTF-8 included, read byte for byte); - the value of every request header except a few that carry no credential (
Accept,Accept-Encoding,Content-Type,User-Agent,anthropic-version,anthropic-beta,openai-beta,x-goog-api-client): a custom header may carry a gateway's token under any name; - the value of a response header whose name is a credential (
Set-Cookie, ...), and the userinfo (user:password@) of the URL; - the value of a query parameter whose name is a credential (
api_key,access_token, ...) or iskey; - in a JSON request or response body, the value under every credential-named key at any depth (
access_token,id_token,api_key,client_secret,password, ...), tool-call arguments and tool schemas included, and that value wherever else the same body repeats it, the model's text included. This catches a token a token exchange returns before llm4s holds it as aSecret. A string that itself holds JSON (a tool call's arguments sent as a string, a JSON-mode answer, a prompt that is a JSON object) is masked the same way, seven levels down at most (a JSON string deeper than that is masked whole); a repeat of a value masked inside such a string is masked only within that string. The rest of a JSON body, prompts and answers included, is written as it was; - in a request body that is text but not JSON (a form-encoded token request), what llm4s's log redaction masks: credential-named fields and values that look like vendor keys. A response that is not JSON (an SSE stream, plain text) only has registered secrets removed: a streamed reply's tool-call arguments are not masked by key;
- in a
LocationorContent-Locationresponse header, the URL's credential query parameters and userinfo.
A query parameter's name is checked percent-decoded (access%5Ftoken), and written as it was sent.
A secret in free text that matches none of these (a key pasted into a prompt) is written. Read a new cassette before committing it.
'''Matching.''' On replay a request matches a recorded one with the same method, URL and body, each masked as above; a JSON body is compared as JSON, and its prompts exactly, after that masking. A text body that is not JSON is compared after its log redaction, so two such bodies that differ only in what that masks match. Headers are not compared unless named in matchHeaders (anthropic-beta, ...), and credential headers never are, so a dummy key replays a recording made with a real one. Each recorded interaction plays once per client, the first unplayed match first. A request that matches none is a Left(CassetteMismatchError), naming the first JSON path at which its body differs from the recording, or the matched header that differs; in every mode but RecordNew it is never sent anywhere.
'''Sharing a file.''' Clients that record to one path in one JVM (Record, RecordIfMissing without the file, RecordNew) share its interactions and write lock, so a target client and a judge client recording to one cassette keep both their exchanges, and a later replay serves both. Open them all before either sends: opening one in Record starts the shared cassette over.
In Record mode a response body is read whole before it is returned, so a recording run does not stream in real time. A body over CassetteHttpClient.MaxBodyBytes, a multipart body, and an exchange that would make the cassette larger than CassetteHttpClient.MaxCassetteBytes are refused with a ValidationError. A failure of the live client is returned as it is and not recorded. The live client stays the caller's to close; close the cassette client too (Using.resource), which releases its hold on a shared cassette.
'''Known limitations.'''
- Replay returns what was written, masked: a value under a credential-named key (a tool-call argument named
token, say a pagination token) replays as[REDACTED], so a flow that sends that value back cannot replay. - A response that is not JSON (a form-encoded OAuth token response) and JSON nested deeper than llm4s's structural redaction reads are only scrubbed of registered secrets.
- What is masked, and so what matches, depends on which secrets the JVM has registered (
Redaction.scrubKnown): a request recorded in a JVM that held a secret may not match one replayed in a JVM that did not. - Embedding clients that take a supplied HTTP client (voyage, jina, cohere, openai embeddings) record and replay too; the
openai,anthropicandbedrockchat clients ignoreoptions.httpClient.
Attributes
- Companion
- object
- Source
- CassetteHttpClient.scala
- Graph
-
- Supertypes
