typescriptbeginner
Claude Messages API (Anthropic SDK)
Send messages to Claude using the official Anthropic SDK with system prompt and user turn.
typescriptPress ⌘/Ctrl + Shift + C to copy
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
export async function askClaude(userMessage: string): Promise<string> {
const msg = await client.messages.create({
model: "claude-opus-4-5",
max_tokens: 1024,
system: "You are a helpful coding assistant. Be concise.",
messages: [{ role: "user", content: userMessage }],
});
const block = msg.content[0];
return block.type === "text" ? block.text : "";
}
// Usage
const answer = await askClaude("What is the difference between RAG and fine-tuning?");
console.log(answer);Sponsored
Build with Claude — Anthropic API
Use Cases
- AI assistant
- text generation
- code review bot
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Anthropic Claude Tool Use Pattern
Implement tool/function calling with Claude using the Anthropic SDK for agentic workflows.
Best for: agentic workflows
#ai#anthropic
typescriptadvanced
RAG Pipeline Implementation
Build a retrieval-augmented generation pipeline that grounds LLM answers in your own documents.
Best for: Grounding LLM answers in private documents
#ai#rag
pythonbeginner
Prompt Template Engineering Patterns
Design reusable, parameterized prompt templates for consistent LLM outputs.
Best for: Consistent LLM outputs
#ai#prompt-engineering
pythonbeginner
Anthropic Streaming with Python
Stream Claude responses token by token using the Anthropic Python SDK with context manager.
Best for: streaming responses
#anthropic#claude