Streaming Anthropic Claude adapter with tool use, thinking, and prompt caching.
Uses stream: true by default so data flows continuously, resetting
the TCP receive timeout with each SSE chunk and avoiding idle-connection
timeouts from intermediary infrastructure.
Features
- Streaming — SSE-based streaming avoids TCP idle timeouts on long requests
- Adaptive thinking — pass
thinking: trueto enable Claude's extended thinking; stream thinking deltas in real time via theon_thinkingcallback - Prompt caching — system prompts and the last tool definition are annotated
with
cache_controlfor Anthropic's prompt caching beta - Usage tracking —
chat_with_usage/2returns token counts including cache metrics - Model-aware defaults —
max_tokensdefaults based on model family
Tool Calling
Pass :tools (list of %LangEx.Tool{}) to enable tool calling.
The adapter returns {:ok, %Message.AI{tool_calls: [...]}} when the
model requests tool use. Use LangEx.Tool.Node to execute them.
LangEx.LLM.Anthropic.chat(messages,
model: "claude-sonnet-4-20250514",
tools: [%LangEx.Tool{name: "get_weather", ...}]
)Options
:thinking— enable adaptive thinking (true/false, defaultfalse):on_thinking—fn(accumulated_thinking_text) -> any()callback:on_token—fn(text_delta) -> any()callback invoked per streamed content token (used by graph streaming's:messagesmode):prompt_caching— enable prompt caching headers (defaulttrue):stream— use SSE streaming (defaulttrue); setfalsefor simple requests:max_tokens— override max tokens (defaults: 64K for sonnet, 128K otherwise)