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, the last tool definition, and a rolling
breakpoint on the last conversation message are annotated with
cache_control, so a long agent loop reuses its cached message prefix each turn - 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):cache_conversation— also mark a rollingcache_controlbreakpoint on the last conversation message (defaulttrue; only applies when:prompt_cachingis on):tool_choice— force tool use::auto(default),:required/:any(must call some tool), or{:tool, name}(must call that tool):stream— use SSE streaming (defaulttrue); setfalsefor simple requests:max_tokens— override max tokens (defaults: 64K for sonnet, 128K otherwise)