An Elixir SDK for Claude Code.
This module provides a simple interface for interacting with Claude Code programmatically.
Basic Usage
# Simple query
for message <- ClaudeAgentSDK.query("Write a hello world function") do
IO.inspect(message)
end
# With options
opts = %ClaudeAgentSDK.Options{
max_turns: 3,
output_format: :json,
system_prompt: "You are a helpful assistant"
}
for message <- ClaudeAgentSDK.query("Build a REST API", opts) do
IO.inspect(message)
endAuthentication
This SDK uses the already-authenticated Claude CLI. You must authenticate manually first:
# In your terminal:
claude loginThe SDK will use the stored authentication from your interactive Claude session.
Summary
Functions
Continues the most recent conversation.
Creates an SDK-based MCP server for in-process tool execution.
Deletes a CLI transcript session and its subagent transcript directory.
Deletes a SessionStore-backed session when supported by the adapter.
Forks a CLI transcript session into a new local session file.
Forks a SessionStore-backed session into a new session.
Looks up metadata for a single CLI transcript session.
Looks up SessionStore-backed session metadata.
Reads visible user/assistant messages from a Claude CLI transcript session.
Reads visible messages from a SessionStore-backed session.
Reads visible messages from a subagent transcript.
Reads visible messages from a SessionStore-backed subagent transcript.
Imports a local Claude JSONL session transcript into a SessionStore adapter.
Lists SDK-managed sessions from ClaudeAgentSDK.SessionStore.
Lists Claude CLI transcript sessions.
Lists SessionStore-backed sessions.
Lists subagent IDs for a CLI transcript session.
Lists subagents in a SessionStore-backed session.
Derives the SessionStore project key for a directory.
Runs a query against Claude Code and returns a stream of messages.
Appends a custom title entry to a CLI transcript session.
Appends a custom title entry to a SessionStore-backed session.
Resumes a specific conversation by session ID.
Appends or clears a tag entry on a CLI transcript session.
Appends or clears a tag on a SessionStore-backed session.
Functions
@spec continue(String.t() | nil, ClaudeAgentSDK.Options.t() | nil) :: Enumerable.t(ClaudeAgentSDK.Message.t())
Continues the most recent conversation.
Parameters
prompt- Optional new prompt to add to the conversationoptions- OptionalClaudeAgentSDK.Optionsstruct with configuration
Examples
# Continue without new prompt
ClaudeAgentSDK.continue()
|> Enum.to_list()
# Continue with new prompt
ClaudeAgentSDK.continue("Now add error handling")
|> Enum.to_list()
@spec create_sdk_mcp_server(keyword()) :: %{ type: :sdk, name: String.t(), version: String.t(), registry_pid: pid() }
Creates an SDK-based MCP server for in-process tool execution.
Unlike external MCP servers that require separate processes, SDK servers run directly within your application, providing:
- Better performance (no subprocess overhead)
- Simpler deployment (no external dependencies)
- Direct function calls to your tool implementations
Parameters
opts- Keyword list with::name- Server name (string, required):version- Server version (string, required):tools- List of tool modules (list of atoms, required)
Returns
A map representing the SDK MCP server with:
:type- Always:sdk:name- Server name:version- Server version (defaults to "1.0.0"):registry_pid- PID of the tool registry GenServer
When :supervisor is omitted, the registry runs under the SDK's internal
SDK MCP supervisor, so it stays alive independently of the creating process.
Examples
defmodule MyTools do
use ClaudeAgentSDK.Tool
deftool :add, "Add two numbers", %{
type: "object",
properties: %{a: %{type: "number"}, b: %{type: "number"}},
required: ["a", "b"]
} do
def execute(%{"a" => a, "b" => b}) do
{:ok, %{"content" => [%{"type" => "text", "text" => "#{a + b}"}]}}
end
end
end
# Create server
server = ClaudeAgentSDK.create_sdk_mcp_server(
name: "calculator",
version: "1.0.0",
tools: [MyTools.Add]
)
# Use in options
options = %ClaudeAgentSDK.Options{
mcp_servers: %{"calc" => server},
allowed_tools: ["mcp__calc__add"]
}
ClaudeAgentSDK.query("Calculate 15 + 27", options)
Deletes a CLI transcript session and its subagent transcript directory.
Deletes a SessionStore-backed session when supported by the adapter.
@spec fork_session( String.t(), keyword() ) :: ClaudeAgentSDK.Session.ForkResult.t()
Forks a CLI transcript session into a new local session file.
@spec fork_session_via_store(String.t(), term(), keyword()) :: ClaudeAgentSDK.Session.ForkResult.t() | {:error, term()}
Forks a SessionStore-backed session into a new session.
@spec get_session_info( String.t(), keyword() ) :: ClaudeAgentSDK.Session.SessionInfo.t() | nil
Looks up metadata for a single CLI transcript session.
@spec get_session_info_from_store(String.t(), term(), keyword()) :: ClaudeAgentSDK.Session.SessionInfo.t() | nil
Looks up SessionStore-backed session metadata.
@spec get_session_messages( String.t(), keyword() ) :: [ClaudeAgentSDK.Session.SessionMessage.t()]
Reads visible user/assistant messages from a Claude CLI transcript session.
@spec get_session_messages_from_store(String.t(), term(), keyword()) :: [ ClaudeAgentSDK.Session.SessionMessage.t() ]
Reads visible messages from a SessionStore-backed session.
@spec get_subagent_messages(String.t(), String.t(), keyword()) :: [ ClaudeAgentSDK.Session.SessionMessage.t() ]
Reads visible messages from a subagent transcript.
@spec get_subagent_messages_from_store(String.t(), String.t(), term(), keyword()) :: [ ClaudeAgentSDK.Session.SessionMessage.t() ]
Reads visible messages from a SessionStore-backed subagent transcript.
Imports a local Claude JSONL session transcript into a SessionStore adapter.
@spec list_saved_sessions(keyword()) :: {:ok, [ClaudeAgentSDK.SessionStore.session_metadata()]} | {:error, term()}
Lists SDK-managed sessions from ClaudeAgentSDK.SessionStore.
Starts the SessionStore automatically when needed.
@spec list_sessions(keyword()) :: [ClaudeAgentSDK.Session.SessionInfo.t()]
Lists Claude CLI transcript sessions.
This mirrors the official Agent SDK list_sessions() surface and reads the
CLI's on-disk JSONL history under ~/.claude/projects.
@spec list_sessions_from_store( term(), keyword() ) :: [ClaudeAgentSDK.Session.SessionInfo.t()]
Lists SessionStore-backed sessions.
Lists subagent IDs for a CLI transcript session.
Lists subagents in a SessionStore-backed session.
Derives the SessionStore project key for a directory.
@spec query( String.t() | Enumerable.t(), ClaudeAgentSDK.Options.t() | nil, term() | nil ) :: Enumerable.t(ClaudeAgentSDK.Message.t())
Runs a query against Claude Code and returns a stream of messages.
Parameters
prompt- The prompt to send to Claudeoptions- OptionalClaudeAgentSDK.Optionsstruct with configuration
Returns
Returns a Stream that yields ClaudeAgentSDK.Message structs.
Examples
# Simple query
ClaudeAgentSDK.query("Write a function to calculate Fibonacci numbers")
|> Enum.to_list()
# With options
opts = %ClaudeAgentSDK.Options{max_turns: 5}
ClaudeAgentSDK.query("Build a web server", opts)
|> Enum.to_list()
Appends a custom title entry to a CLI transcript session.
Appends a custom title entry to a SessionStore-backed session.
@spec resume(String.t(), String.t() | nil, ClaudeAgentSDK.Options.t() | nil) :: Enumerable.t(ClaudeAgentSDK.Message.t())
Resumes a specific conversation by session ID.
Parameters
session_id- The session ID to resumeprompt- Optional new prompt to add to the conversationoptions- OptionalClaudeAgentSDK.Optionsstruct with configuration
Examples
ClaudeAgentSDK.resume("550e8400-e29b-41d4-a716-446655440000", "Add tests")
|> Enum.to_list()
Appends or clears a tag entry on a CLI transcript session.
@spec tag_session_via_store(String.t(), String.t() | nil, term(), keyword()) :: :ok | {:error, term()}
Appends or clears a tag on a SessionStore-backed session.