Conjure.API.Anthropic (Conjure v0.1.1-alpha)

View Source

Helpers for building Anthropic Skills API requests.

This module provides pure functions for constructing API parameters when using Anthropic's hosted Skills execution. It does NOT make HTTP calls - users provide their own HTTP client.

Beta Requirements

The Skills API requires beta headers. Use beta_headers/0 to get the required headers for your HTTP client:

headers = [
  {"x-api-key", api_key},
  {"anthropic-version", "2023-06-01"}
] ++ Conjure.API.Anthropic.beta_headers()

Skill Types

  • :anthropic - Pre-built skills: "xlsx", "pptx", "docx", "pdf"
  • :custom - User-uploaded skills with generated IDs

Example

# Build container config for skills
{:ok, container} = Conjure.API.Anthropic.container_config([
  {:anthropic, "xlsx", "latest"},
  {:anthropic, "pptx", "latest"},
  {:custom, "skill_01AbCdEfGhIjKlMnOpQrStUv", "latest"}
])

# Build complete request
request = Conjure.API.Anthropic.build_request(
  messages,
  container,
  model: "claude-sonnet-4-5-20250929",
  max_tokens: 4096
)

References

Summary

Functions

Get the required beta headers for Skills API requests.

Build the assistant message from a response for continuing conversation.

Build a complete messages API request body.

Get the code execution tool definition.

Build the container configuration for skills.

Build container config, raising on error.

Check if response indicates end_turn (conversation complete).

Extract file IDs from a response.

Extract text content from a response.

Parse an API response, extracting relevant fields.

Check if response indicates a pause_turn (long-running operation).

Add a container ID to an existing container config.

Types

parsed_response()

@type parsed_response() :: %{
  content: list(),
  stop_reason: String.t(),
  container_id: String.t() | nil,
  file_ids: [String.t()],
  usage: map() | nil
}

skill_spec()

@type skill_spec() :: {skill_type(), skill_id :: String.t(), version :: String.t()}

skill_type()

@type skill_type() :: :anthropic | :custom

Functions

beta_headers()

@spec beta_headers() :: [{String.t(), String.t()}]

Get the required beta headers for Skills API requests.

These headers must be included in all API requests that use Skills or code execution features.

Example

headers = Conjure.API.Anthropic.beta_headers()
# => [{"anthropic-beta", "code-execution-2025-08-25,skills-2025-10-02,files-api-2025-04-14"}]

build_assistant_message(map)

@spec build_assistant_message(map()) :: map()

Build the assistant message from a response for continuing conversation.

Use this when handling pause_turn to build the next messages array.

Example

# Response came back with pause_turn
assistant_message = build_assistant_message(response)
updated_messages = messages ++ [assistant_message]
# Continue with updated_messages

build_request(messages, container_config, opts \\ [])

@spec build_request([map()], map(), keyword()) :: map()

Build a complete messages API request body.

Combines messages, container config, and options into a complete request body ready to be JSON-encoded.

Options

  • :model - Model to use (default: "claude-sonnet-4-5-20250929")
  • :max_tokens - Maximum tokens in response (default: 4096)
  • :system - System prompt
  • :tools - Additional tools (code_execution is added automatically)
  • :metadata - Request metadata

Example

{:ok, container} = container_config([{:anthropic, "xlsx", "latest"}])

request = build_request(
  [%{"role" => "user", "content" => "Create a spreadsheet"}],
  container,
  model: "claude-sonnet-4-5-20250929",
  max_tokens: 8192,
  system: "You are a helpful assistant."
)

code_execution_tool()

@spec code_execution_tool() :: map()

Get the code execution tool definition.

This tool definition must be included in the tools array when making API requests with Skills.

Example

tools = [Conjure.API.Anthropic.code_execution_tool()]

container_config(skills)

@spec container_config([skill_spec()]) :: {:ok, map()} | {:error, Conjure.Error.t()}

Build the container configuration for skills.

Accepts a list of skill specifications and returns the container parameter for the API request. Maximum 8 skills per request.

Skill Specification Format

Each skill is a tuple: {type, skill_id, version}

  • type - :anthropic for pre-built skills, :custom for uploaded
  • skill_id - Short name ("xlsx") or generated ID ("skill_01...")
  • version - Version string or "latest"

Example

{:ok, container} = Conjure.API.Anthropic.container_config([
  {:anthropic, "xlsx", "latest"},
  {:custom, "skill_01AbCdEfGhIjKlMnOpQrStUv", "v1"}
])

# Returns:
# %{
#   "skills" => [
#     %{"type" => "anthropic", "skill_id" => "xlsx", "version" => "latest"},
#     %{"type" => "custom", "skill_id" => "skill_01...", "version" => "v1"}
#   ]
# }

container_config!(skills)

@spec container_config!([skill_spec()]) :: map()

Build container config, raising on error.

end_turn?(arg1)

@spec end_turn?(map() | parsed_response()) :: boolean()

Check if response indicates end_turn (conversation complete).

extract_file_ids(arg1)

@spec extract_file_ids(map()) :: [String.t()]

Extract file IDs from a response.

Skills that create files (xlsx, pptx, pdf) include file references in the response content. These IDs can be used with the Files API to download the created documents.

Example

file_ids = extract_file_ids(response)
# => ["file_01AbCdEf...", "file_02GhIjKl..."]

Enum.each(file_ids, fn file_id ->
  {:ok, content, filename} = Conjure.Files.Anthropic.download(file_id, api_callback)
  File.write!(filename, content)
end)

extract_text(arg1)

@spec extract_text(map()) :: String.t()

Extract text content from a response.

parse_response(response)

@spec parse_response(map()) :: {:ok, parsed_response()} | {:error, term()}

Parse an API response, extracting relevant fields.

Extracts content, stop_reason, container_id, file_ids, and usage from the response.

Example

{:ok, parsed} = parse_response(api_response)

if pause_turn?(parsed) do
  # Continue conversation
end

file_ids = parsed.file_ids

pause_turn?(arg1)

@spec pause_turn?(map() | parsed_response()) :: boolean()

Check if response indicates a pause_turn (long-running operation).

When true, the conversation should continue by sending the response content back to the API with the same container ID.

Example

{:ok, parsed} = parse_response(response)

if pause_turn?(parsed) do
  # Long-running operation in progress, continue conversation
  updated_container = with_container_id(container, parsed.container_id)
  # ... continue loop
end

with_container_id(container_config, container_id)

@spec with_container_id(map(), String.t()) :: map()

Add a container ID to an existing container config.

Use this for multi-turn conversations to reuse the same container.

Example

# First request - no container ID
{:ok, container} = container_config([{:anthropic, "xlsx", "latest"}])

# Parse container ID from response
container_id = get_in(response, ["container", "id"])

# Subsequent requests - reuse container
container_with_id = with_container_id(container, container_id)