Class AnthropicBaseFormatter

java.lang.Object
io.agentscope.core.formatter.AbstractBaseFormatter<com.anthropic.models.messages.MessageParam,Object,com.anthropic.models.messages.MessageCreateParams.Builder>
io.agentscope.extensions.model.anthropic.formatter.AnthropicBaseFormatter
All Implemented Interfaces:
io.agentscope.core.formatter.Formatter<com.anthropic.models.messages.MessageParam,Object,com.anthropic.models.messages.MessageCreateParams.Builder>
Direct Known Subclasses:
AnthropicChatFormatter, AnthropicMultiAgentFormatter

public abstract class AnthropicBaseFormatter extends io.agentscope.core.formatter.AbstractBaseFormatter<com.anthropic.models.messages.MessageParam,Object,com.anthropic.models.messages.MessageCreateParams.Builder>
Abstract base formatter for Anthropic API with shared logic for handling Anthropic-specific requirements.

This class handles:

  • System message extraction and application (Anthropic requires system via system parameter)
  • Tool choice configuration with GenerateOptions
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    List<com.anthropic.models.messages.MessageParam>
    applyCacheControl(List<com.anthropic.models.messages.MessageParam> formattedMessages)
    Apply the automatic cache control strategy to formatted messages.
    void
    applyOptions(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions)
    Apply generation options to Anthropic request parameters.
    void
    applySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages)
    Extract and apply system message if present.
    void
    applySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages, boolean cacheControlEnabled)
    Extract and apply system message if present, optionally marking it for prompt caching.
    void
    applyTools(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.model.ToolSchema> tools)
    Apply tool schemas to Anthropic request parameters.
    void
    cacheTtl(String cacheTtl)
    Configure the TTL applied to prompt-caching markers (e.g.

    Methods inherited from class io.agentscope.core.formatter.AbstractBaseFormatter

    convertToolResultToString, doFormat, doFormat, extractTextContent, extractTextContent, format, format, formatRoleLabel, getOptionOrDefault, hasMediaContent, saveBase64DataToTempFile, shouldBypassHistory

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.agentscope.core.formatter.Formatter

    applyToolChoice, applyToolChoice, applyTools, parseResponse
  • Field Details

  • Constructor Details

    • AnthropicBaseFormatter

      protected AnthropicBaseFormatter()
  • Method Details

    • applyOptions

      public void applyOptions(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions)
      Apply generation options to Anthropic request parameters.
      Parameters:
      paramsBuilder - Anthropic request parameters builder
      options - Generation options to apply
      defaultOptions - Default options to use if options parameter is null
    • applyTools

      public void applyTools(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.model.ToolSchema> tools)
      Apply tool schemas to Anthropic request parameters. This method uses the options saved from applyOptions to apply tool choice configuration.
      Parameters:
      paramsBuilder - Anthropic request parameters builder
      tools - List of tool schemas to apply (may be null or empty)
    • applySystemMessage

      public void applySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages)
      Extract and apply system message if present. Anthropic API requires system message to be set via the system parameter, not as a message.

      This method is called by Model to extract the first system message from the messages list and apply it to the system parameter.

      Parameters:
      paramsBuilder - Anthropic request parameters builder
      messages - All messages including potential system message
    • applySystemMessage

      public void applySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages, boolean cacheControlEnabled)
      Extract and apply system message if present, optionally marking it for prompt caching.

      When cacheControlEnabled is true, the system prompt is sent as a text block carrying cache_control: {"type": "ephemeral"} so long system prompts can be cached by Anthropic.

      Parameters:
      paramsBuilder - Anthropic request parameters builder
      messages - All messages including potential system message
      cacheControlEnabled - whether prompt caching is enabled
    • cacheTtl

      public void cacheTtl(String cacheTtl)
      Configure the TTL applied to prompt-caching markers (e.g. "1h").
    • applyCacheControl

      public List<com.anthropic.models.messages.MessageParam> applyCacheControl(List<com.anthropic.models.messages.MessageParam> formattedMessages)
      Apply the automatic cache control strategy to formatted messages.

      Adds cache_control: {"type": "ephemeral"} to the last cacheable content block of the last message. Anthropic caches everything up to and including the marked block, so this caches the entire conversation prefix across turns.

      Parameters:
      formattedMessages - the formatted Anthropic messages
      Returns:
      the messages with cache control applied