Skip to main content

Anthropic Claude

LogicGrid talks to the Anthropic Messages API directly — no SDK required.

Use it

There are two equivalent ways to instantiate the Anthropic LLM client.

using LogicGrid.Core.Llm;

var llm = LlmClientBase.Anthropic(
apiKey: Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")!,
model: "claude-3-5-haiku-20241022");
ParameterTypeDefaultNotes
apiKeystring(required)Anthropic API key, sent as the x-api-key header.
modelstring"claude-3-5-haiku-20241022"Any Claude model. The anthropic-version header is set automatically.

Option 2 — direct construction

using LogicGrid.Core.Providers;

var llm = new AnthropicClient(
apiKey: Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")!,
defaultModel: "claude-3-5-haiku-20241022");
ParameterTypeDefaultNotes
apiKeystring(required)Same as the factory's apiKey.
defaultModelstring"claude-3-5-haiku-20241022"The model used when the agent or call site doesn't override it.

The factory and the constructor produce equivalent clients. Use direct construction when you need an injected HttpClient (for retries, proxies, or testing). The caller is responsible for setting the Authorization: Bearer header.

Models currently available

Anthropic publishes new Claude models regularly — always consult the official models page for the current list. Common picks at the time of writing:

ModelNotes
claude-3-5-sonnet-20241022Strong general model.
claude-3-5-haiku-20241022Fast and cheap.
claude-3-opus-20240229Earlier flagship; strong on long-form reasoning.
claude-3-haiku-20240307Cheapest Claude.

LogicGrid sends the anthropic-version header automatically. New Claude models work as soon as Anthropic releases them — no library update required.

Long context

Several Claude models accept long context windows (Claude 3.5 Sonnet currently supports 200K input tokens). Useful for one-shot summarisation or analysis where you don't want to chunk and retrieve. See the models page for each model's exact limit.

System prompts

Anthropic's API takes the system prompt as a top-level field rather than as a message. LogicGrid handles the translation for you, so the SystemPrompt you set on Agent<T> (or override on AgentBase<T>) behaves the same way as on every other provider.

Tool calling

Anthropic supports native tool calling. It's the most reliable native implementation across providers.

protected override IToolCallingStrategy ToolCallingStrategy
=> new NativeToolCallingStrategy();

Full example — calculator tool over Claude

using LogicGrid.Core.Agents;
using LogicGrid.Core.Llm;
using LogicGrid.Core.Tools;
using LogicGrid.Tools.Tools;

var llm = LlmClientBase.Anthropic(
apiKey: Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")!,
model: "claude-3-5-haiku-20241022");

IAgent math = new Agent<string>(
name: "Mathlete",
description: "Solves arithmetic.",
systemPrompt: "Use the calculator tool when the user asks for a number.",
llm: llm,
tools: new ToolBase[] { new CalculatorTool() });

Console.WriteLine(
await math.RunAsync(
input: "What is (17 * 23) + 91?",
ctx: new AgentContext().WithLogging()));
09:14:02.118 [INF] [Mathlete] started
09:14:02.420 [INF] [Mathlete] tool call → calculator { "expression": "(17 * 23) + 91" }
09:14:02.430 [INF] [Mathlete] tool result | 482
09:14:03.180 [INF] [Mathlete] completed | output: (17 * 23) + 91 = 482.

(17 * 23) + 91 = 482.

Background: Tool calling strategy.

Cost tracking

AnthropicClient.Pricing returns per-token rates for known models. Live rates: anthropic.com/pricing.

Troubleshooting

  • 401 — invalid API key. Check console.anthropic.com.
  • overloaded_error — Claude is busy. Retry with backoff. The default retry policy handles this automatically.
  • invalid_request_error — usually a malformed message history. Check that tool result messages are paired with the corresponding tool calls.