MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / chat_with_tools_anthropic

Method chat_with_tools_anthropic

atomic-repository/src/ai/tools.rs:180–308  ·  view source on GitHub ↗
(
        &self,
        messages: &[ToolMessage],
        tools: &[ToolDefinition],
        max_tokens: u32,
    )

Source from the content-addressed store, hash-verified

178 // ── Anthropic ───────────────────────────────────────────────
179
180 async fn chat_with_tools_anthropic(
181 &self,
182 messages: &[ToolMessage],
183 tools: &[ToolDefinition],
184 max_tokens: u32,
185 ) -> Result<ToolAwareResponse, AiError> {
186 let url = format!("{}/messages", self.base_url);
187
188 // Extract system prompt (Anthropic uses a top-level field).
189 let system_text: String = messages
190 .iter()
191 .filter_map(|m| match m {
192 ToolMessage::System { content } => Some(content.as_str()),
193 _ => None,
194 })
195 .collect::<Vec<_>>()
196 .join("\n\n");
197
198 // Build message array (skip System messages).
199 let api_messages = anthropic_messages(messages);
200
201 // Build tools array.
202 let api_tools: Vec<serde_json::Value> = tools
203 .iter()
204 .map(|t| {
205 json!({
206 "name": t.name,
207 "description": t.description,
208 "input_schema": t.parameters,
209 })
210 })
211 .collect();
212
213 let mut body = json!({
214 "model": self.model,
215 "max_tokens": max_tokens,
216 "messages": api_messages,
217 });
218
219 if !system_text.is_empty() {
220 // Use content block format with cache_control for prompt caching.
221 // The system prompt + tools are static across turns — caching them
222 // means turns 2+ pay ~0 for the system prompt.
223 body["system"] = json!([
224 {
225 "type": "text",
226 "text": system_text,
227 "cache_control": { "type": "ephemeral" }
228 }
229 ]);
230 }
231 if !api_tools.is_empty() {
232 body["tools"] = json!(api_tools);
233 }
234
235 let client = reqwest::Client::new();
236 let resp = client
237 .post(&url)

Callers 1

chat_with_toolsMethod · 0.80

Calls 13

anthropic_messagesFunction · 0.85
postMethod · 0.80
textMethod · 0.80
as_u64Method · 0.80
iterMethod · 0.45
as_strMethod · 0.45
is_emptyMethod · 0.45
jsonMethod · 0.45
headerMethod · 0.45
is_successMethod · 0.45
statusMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected