Commands & Snippets
FreshQuick reference for common commands and code patterns.
Claude API
Installation
bash
# Python
pip install anthropic
# Node.js
npm install @anthropic-ai/sdkEnvironment Setup
bash
# Set API key
export ANTHROPIC_API_KEY="sk-ant-api03-..."
# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-api03-..."Basic Messages
python
import anthropic
client = anthropic.Anthropic()
# Simple message
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
)
# With system prompt
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Hello!"}]
)Streaming
python
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)Tool Use
python
tools = [{
"name": "get_weather",
"description": "Get weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}]
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Weather in Boston?"}]
)
# Check for tool use
for block in response.content:
if block.type == "tool_use":
print(f"Tool: {block.name}")
print(f"Input: {block.input}")Claude Code CLI
Session Commands
bash
# Start new session
claude
# Resume last session
claude --resume
# Continue with context
claude --continue
# Start with prompt
claude "explain this code"In-Session Commands
/help - Show help
/clear - Clear context
/exit - Exit session
/compact - Compress contextCommon Prompts
"Look at [file] and explain how it works"
"Create a new [component/function] that does [X]"
"Fix the error in [file]"
"Write tests for [module]"
"Refactor this to use [pattern]"
"Create a commit for these changes"MCP Commands
Server Development
bash
# Install MCP SDK
pip install mcp
# Run server directly
python server.py
# Run with uvicorn (HTTP)
uvicorn server:app --reloadClient Configuration
json
{
"mcpServers": {
"my-server": {
"command": "python",
"args": ["path/to/server.py"],
"env": {
"API_KEY": "your-key"
}
}
}
}Config File Locations
| Platform | Location |
|---|---|
| macOS | ~/Library/Application Support/Claude/claude_desktop_config.json |
| Windows | %APPDATA%\Claude\claude_desktop_config.json |
| Linux | ~/.config/Claude/claude_desktop_config.json |
RAG Patterns
Chunking
python
def chunk_text(text, size=500, overlap=50):
chunks = []
start = 0
while start < len(text):
end = start + size
chunks.append(text[start:end])
start = end - overlap
return chunksEmbedding
python
import voyageai
client = voyageai.Client()
# Embed documents
embeddings = client.embed(
texts=chunks,
model="voyage-3",
input_type="document"
).embeddings
# Embed query
query_embedding = client.embed(
texts=[query],
model="voyage-3",
input_type="query"
).embeddings[0]Hybrid Search
python
from rank_bm25 import BM25Okapi
from sklearn.metrics.pairwise import cosine_similarity
def hybrid_search(query, chunks, embeddings):
# Semantic
q_emb = embed_query(query)
semantic_scores = cosine_similarity([q_emb], embeddings)[0]
# BM25
bm25 = BM25Okapi([c.split() for c in chunks])
bm25_scores = bm25.get_scores(query.split())
# Combine (RRF)
return reciprocal_rank_fusion(semantic_scores, bm25_scores)Model Reference
| Model | Best For |
|---|---|
| claude-opus-4-5-20251101 | Complex reasoning, analysis |
| claude-sonnet-4-20250514 | Balanced performance |
| claude-haiku-3-20240307 | Fast, simple tasks |
Token Limits
| Model | Context | Max Output |
|---|---|---|
| Claude 3.5 Sonnet | 200K | 8K |
| Claude 3 Opus | 200K | 4K |
| Claude 3 Haiku | 200K | 4K |