Conjure.API (Conjure v0.1.1-alpha)

View Source

Helpers for Claude API integration.

This module provides utilities for building API requests and parsing responses. It does not make HTTP calls - use your preferred HTTP client.

Example

# Build request components
system_prompt = Conjure.API.build_system_prompt("You are helpful.", skills)
tools = Conjure.API.build_tools_param(skills)

# Make API call with your client
response = MyApp.Claude.call(%{
  model: "claude-sonnet-4-5-20250929",
  system: system_prompt,
  messages: messages,
  tools: tools
})

# Parse response
{:ok, parsed} = Conjure.API.parse_response(response)

Summary

Functions

Build a complete messages array for an API request.

Build the system prompt with skills fragment appended.

Build the tools array for the API request.

Check if the response is an end_turn (conversation complete).

Estimate token count for a request.

Extract the full text content from a response.

Format tool results for the next API request.

Parse content blocks from an API response.

Check if a response requires tool execution.

Types

parsed_response()

@type parsed_response() :: %{
  text_blocks: [String.t()],
  tool_uses: [Conjure.ToolCall.t()],
  stop_reason: String.t() | nil,
  usage: map() | nil
}

Functions

build_messages(history)

@spec build_messages([map()]) :: [map()]

Build a complete messages array for an API request.

Handles the conversation history format expected by Claude.

build_system_prompt(base_prompt, skills, opts \\ [])

@spec build_system_prompt(String.t(), [Conjure.Skill.t()], keyword()) :: String.t()

Build the system prompt with skills fragment appended.

Options

  • :include_instructions - Include skill usage instructions (default: true)

build_tools_param(skills, opts \\ [])

@spec build_tools_param(
  [Conjure.Skill.t()],
  keyword()
) :: [map()]

Build the tools array for the API request.

Options

  • :only - Only include these tool names
  • :except - Exclude these tool names

end_turn?(arg1)

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

Check if the response is an end_turn (conversation complete).

estimate_tokens(content)

@spec estimate_tokens(String.t() | map()) :: pos_integer()

Estimate token count for a request.

This is a rough estimate assuming ~4 characters per token.

extract_text(response)

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

Extract the full text content from a response.

format_tool_results_message(results)

@spec format_tool_results_message([Conjure.ToolResult.t()]) :: map()

Format tool results for the next API request.

Returns a message map with role "user" and tool_result content blocks.

parse_response(response)

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

Parse content blocks from an API response.

Extracts text blocks, tool uses, and metadata from the response.

requires_tool_execution?(arg1)

@spec requires_tool_execution?(map()) :: boolean()

Check if a response requires tool execution.