Anthropic Pocket Book

Anthropic Pocket Book

Claude models • Messages API • Prompting patterns • Safety & filters • Tools & function calls • Ops tips

Section 1 — Fundamentals

1) What is Anthropic (Claude)?

Anthropic provides the Claude family of large language models for chat, knowledge tasks, coding, and agents. You access Claude via the Messages API, SDKs, and integrations. Core ideas: helpfulness, honesty, and safety-by-default.

# Install Python SDK
pip install anthropic

2) Key Concepts

  • Model: e.g., claude-3, claude-3.5 variants (pick for cost/latency/capability).
  • Messages: array of user/assistant turns; you send, model responds.
  • Tools: declare functions the model can call; you execute and return results.
  • System prompt: sets persona, rules, and constraints for the session.

3) Simple Completion (Python)

from anthropic import Anthropic
client = Anthropic(api_key="YOUR_ANTHROPIC_API_KEY")

resp = client.messages.create(
  model="claude-3-opus-20240229",
  max_tokens=300,
  messages=[{"role":"user","content":"Explain vector databases in 5 bullets."}]
)
print(resp.content[0].text)

Replace the model name with the current recommended one for your region/tier.

4) cURL Example

curl https://api.anthropic.com/v1/messages \
 -H "x-api-key: $ANTHROPIC_API_KEY" \
 -H "content-type: application/json" \
 -H "anthropic-version: 2023-06-01" \
 -d '{
  "model":"claude-3-haiku-20240307",
  "max_tokens":200,
  "messages":[{"role":"user","content":"Give me 3 tips to optimize a REST API."}]
}'

5) System Prompt Pattern

Pin down tone, format, and boundaries up front.

SYSTEM:
You are a concise technical assistant. Prefer bullet points, cite file names in code, and refuse unsafe requests.

Section 2 — Prompting, Tools & Retrieval

6) Few-shot Prompting

Show short examples to steer style and structure. Keep examples minimal and domain-relevant.

messages=[
 {"role":"user","content":"Format a changelog entry for: fix CORS issue"},
 {"role":"assistant","content":"- fix(cors): allow credentials on /api/* (closes #123)"},
 {"role":"user","content":"Format a changelog entry for: upgrade Node to 20.x"}
]

7) Tool Use (Function Calling) — Pattern

Declare your tools’ JSON schema; the model replies with a tool call. Your app executes it and returns results as a new message.

tools=[{
  "name":"get_weather",
  "description":"Get weather by city",
  "input_schema":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}
}]
# Send messages with tools=tools; when response includes a tool call, run it and send the result back.

8) RAG (Retrieval-Augmented Generation)

Store documents in a vector DB (e.g., Pinecone, Weaviate, FAISS). At query time: embed → search → feed top chunks into the prompt.

SYSTEM: Answer strictly from the provided context. If unknown, say so.
CONTEXT:
- [doc1] ...
- [doc2] ...
USER: Summarize the key steps.

9) Structured Outputs (JSON)

Ask for strict JSON with a small schema; validate before use.

USER: Extract fields as JSON: {"title":string,"priority":"low|med|high","tags":string[]}

10) Rate Limits & Retries

Implement exponential backoff on 429/5xx, and respect per-minute token limits. Log prompt/response sizes to control costs.

Section 3 — Safety, Policy & Ops

11) Safety Filters

Design prompts to avoid unsafe content; handle refusals gracefully. Use allow/deny lists and post-process outputs for your domain rules.

12) Secrets & Keys

Store API keys in a secrets manager (e.g., AWS Secrets Manager). Never ship keys to the client; proxy through your backend for web apps.

13) Observability

Log request IDs, latency, tokens, and user/session correlation. Sample prompts/responses (with redaction) to debug quality regressions.

14) Cost Controls

  • Choose smaller models for high-volume tasks; reserve larger for complex cases.
  • Truncate context; summarize long threads; reuse short memory.
  • Cache responses to frequent prompts; dedupe retries.

Section 4 — Quick Q&A

15) Interview Q&A — 8 Quick Ones

1) Claude vs traditional NLP? Foundation models generalize across tasks; prompt/ground instead of bespoke pipelines.

2) When to use tools? For fresh data, calculations, or actions the model can’t perform internally.

3) Guardrails? System prompts, content filters, schema validation, and allow/deny rules.

4) Reduce hallucinations? Provide context, instruct to say “don’t know,” and verify with tools.

5) Streaming? Use SDK streaming to render tokens as they arrive for better UX.

6) Eval quality? Define task-specific metrics and golden datasets; run A/B against baselines.

7) Error handling? Backoff on 429/5xx, timeouts, and circuit breakers; surface actionable messages.

8) Data privacy? Don’t send secrets unnecessarily; mask PII; follow org retention policies.

16) Quick Starter (Node.js)

npm i @anthropic-ai/sdk
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

const resp = await client.messages.create({
  model: "claude-3-sonnet-20240229",
  max_tokens: 200,
  messages: [{ role: "user", content: "Summarize S3 lifecycle rules in 4 bullets." }]
});
console.log(resp.content[0].text);