Class NaruAnthropicRequestSerializer

java.lang.Object
net.thevpc.naru.ext.models.anthropic.NaruAnthropicRequestSerializer
All Implemented Interfaces:
NaruCacheAwareRequestSerializer, NaruModelRequestSerializer

public class NaruAnthropicRequestSerializer extends Object implements NaruCacheAwareRequestSerializer
Serializes a NaruModelRequest into the Anthropic Messages API wire format:
 POST {base}/v1/messages
 {
   "model": "...",
   "max_tokens": 4096,                       // REQUIRED by Anthropic
   "system": "You are...",                   // top-level, joined system messages
   "messages": [
     {"role": "user", "content": "..."}                         // plain text
     {"role": "user", "content": [{"type":"text",...},{"type":"image",...}]} // multimodal
     {"role": "assistant", "content": [...{"type":"tool_use","id":...,"name":...,"input":{...}}]}
     {"role": "user", "content": [{"type":"tool_result","tool_use_id":"...","content":"..."}]}
   ],
   "tools": [{"name":"...","description":"...","input_schema":{...}}],
   "temperature": ..., "top_p": ..., "stop_sequences": [...]
 }
 

Prompt caching

Anthropic's cache is controlled by cache_control breakpoints on content blocks; a breakpoint caches everything from the start of the request up to and including the block it sits on. Two things follow from that, and both are handled here rather than by the caller:
  • Block form is required. A cache_control cannot ride on a bare string, so any field that carries a marker is emitted as an array of blocks. Fields with no marker keep the compact string form, so a request with caching off is byte-identical to one from before this class learned about it.
  • Wire order is not segment order. Anthropic evaluates the prefix as tools, then system, then messages — regardless of the order the caller laid its segments out in. Markers are therefore resolved against wire position here. Getting this wrong would place every marker one position earlier than intended, silently caching less than the caller asked for.
  • Field Details

    • MAX_CACHE_BREAKPOINTS

      public static final int MAX_CACHE_BREAKPOINTS
      Anthropic rejects a request carrying more than four cache breakpoints. The limit is applied when choosing markers, not by silently dropping extras after the fact, so the surviving markers are the useful ones.
      See Also:
  • Constructor Details

    • NaruAnthropicRequestSerializer

      public NaruAnthropicRequestSerializer()
  • Method Details