Programování
Expert
Ostatní
Advanced AI Features - Deep Dive do Pokročilých Funkcí
Komplexní průvodce pokročilými funkcemi AI nástrojů - od extended context a multimodality po tool use, streaming, a fine-tuning. Naučte se využívat plný potenciál moderních AI systémů.
Prompt text
Délka: Dlouhý
Jsi expert na pokročilé funkce AI nástrojů a jejich praktické využití. Pomoz mi porozumět a efektivně využívat advanced features pro můj use case.
Můj kontext:
- Primární AI nástroj: [NÁSTROJ - např. "Claude Pro", "ChatGPT Plus", "Cursor", "Gemini Advanced"]
- Use case: [POUŽITÍ - např. "software development", "content creation", "data analysis", "research"]
- Aktuální úroveň: [ZKUŠENOST - např. "používám jen základní chat", "znám prompt engineering", "chci pokročilé techniky"]
- Cíl: [CÍL - např. "maximalizovat produktivitu", "zlepšit kvalitu outputs", "automatizovat workflow"]
- Specifické potřeby: [POTŘEBY - např. "práce s velkými dokumenty", "analýza kódu", "kreativní projekty"]
Vytvoř mi:
1. **Extended Context Management**
- Jak efektivně využít dlouhý context window
- Techniky pro context compression
- Strategie pro udržení relevance
- Best practices pro large document processing
2. **Multimodal Capabilities**
- Vision (image understanding)
- Audio processing
- PDF and document analysis
- Kombinace different modalities
3. **Tool Use & Function Calling**
- Jak AI nástroje používají external tools
- Custom function implementation
- Integration patterns
- Real-world automation examples
4. **Streaming & Real-Time Processing**
- Streaming responses
- Incremental processing
- Real-time collaboration
- Performance optimization
5. **Advanced Prompt Engineering**
- System instructions
- Few-shot learning
- Chain-of-thought prompting
- Role prompting techniques
6. **Fine-Tuning & Customization**
- Custom instructions
- Model fine-tuning (kde dostupné)
- Domain-specific optimization
- Knowledge base integration
7. **API Integration & Automation**
- API usage patterns
- Batch processing
- Workflow automation
- Cost optimization
8. **Practical Examples**
Pro každou advanced feature:
- Real-world use case
- Step-by-step implementation
- Code examples (if applicable)
- Měřitelné benefity
9. **Tool-Specific Features**
**Claude-specific:**
- Extended Context (200K tokens)
- Claude Code capabilities
- Analysis tool
- Artifacts
**ChatGPT-specific:**
- DALL-E integration
- Code Interpreter
- Web browsing
- GPTs creation
**Cursor-specific:**
- Composer Mode
- Multi-file editing
- Terminal integration
- Custom rules
**Gemini-specific:**
- 1M token context
- Multimodal capabilities
- Google Search integration
- Workspace integration
10. **Comparison & Selection Guide**
- Which feature for which task
- Cost-benefit analysis
- Performance benchmarks
- Migration strategies
Zaměř se na:
✅ Praktické, implementovatelné příklady
✅ Měřitelné improvement metriky
✅ Cost-effectiveness
✅ Real-world použití
✅ Best practices a pitfalls
Můj kontext:
- Primární AI nástroj: [NÁSTROJ - např. "Claude Pro", "ChatGPT Plus", "Cursor", "Gemini Advanced"]
- Use case: [POUŽITÍ - např. "software development", "content creation", "data analysis", "research"]
- Aktuální úroveň: [ZKUŠENOST - např. "používám jen základní chat", "znám prompt engineering", "chci pokročilé techniky"]
- Cíl: [CÍL - např. "maximalizovat produktivitu", "zlepšit kvalitu outputs", "automatizovat workflow"]
- Specifické potřeby: [POTŘEBY - např. "práce s velkými dokumenty", "analýza kódu", "kreativní projekty"]
Vytvoř mi:
1. **Extended Context Management**
- Jak efektivně využít dlouhý context window
- Techniky pro context compression
- Strategie pro udržení relevance
- Best practices pro large document processing
2. **Multimodal Capabilities**
- Vision (image understanding)
- Audio processing
- PDF and document analysis
- Kombinace different modalities
3. **Tool Use & Function Calling**
- Jak AI nástroje používají external tools
- Custom function implementation
- Integration patterns
- Real-world automation examples
4. **Streaming & Real-Time Processing**
- Streaming responses
- Incremental processing
- Real-time collaboration
- Performance optimization
5. **Advanced Prompt Engineering**
- System instructions
- Few-shot learning
- Chain-of-thought prompting
- Role prompting techniques
6. **Fine-Tuning & Customization**
- Custom instructions
- Model fine-tuning (kde dostupné)
- Domain-specific optimization
- Knowledge base integration
7. **API Integration & Automation**
- API usage patterns
- Batch processing
- Workflow automation
- Cost optimization
8. **Practical Examples**
Pro každou advanced feature:
- Real-world use case
- Step-by-step implementation
- Code examples (if applicable)
- Měřitelné benefity
9. **Tool-Specific Features**
**Claude-specific:**
- Extended Context (200K tokens)
- Claude Code capabilities
- Analysis tool
- Artifacts
**ChatGPT-specific:**
- DALL-E integration
- Code Interpreter
- Web browsing
- GPTs creation
**Cursor-specific:**
- Composer Mode
- Multi-file editing
- Terminal integration
- Custom rules
**Gemini-specific:**
- 1M token context
- Multimodal capabilities
- Google Search integration
- Workspace integration
10. **Comparison & Selection Guide**
- Which feature for which task
- Cost-benefit analysis
- Performance benchmarks
- Migration strategies
Zaměř se na:
✅ Praktické, implementovatelné příklady
✅ Měřitelné improvement metriky
✅ Cost-effectiveness
✅ Real-world použití
✅ Best practices a pitfalls
Příklad použití
Vstup:
Primární AI nástroj: Claude Pro
Use case: Full-stack software development (Next.js, Python, databases)
Aktuální úroveň: Používám Claude denně, znám základní prompt engineering, ale nevyužívám extended context a další pokročilé funkce
Cíl: Chci maximalizovat produktivitu při vývoji a využít plný potenciál Claude Pro
Specifické potřeby: Práce s velkými codebase, multi-file refactoring, code review, architektura design
Use case: Full-stack software development (Next.js, Python, databases)
Aktuální úroveň: Používám Claude denně, znám základní prompt engineering, ale nevyužívám extended context a další pokročilé funkce
Cíl: Chci maximalizovat produktivitu při vývoji a využít plný potenciál Claude Pro
Specifické potřeby: Práce s velkými codebase, multi-file refactoring, code review, architektura design
Výstup:
# Advanced AI Features Deep Dive - Claude Pro for Software Development
## Maximální Využití Pokročilých Funkcí 🚀
---
## 1. Extended Context Management (200K Tokens)
### 🎯 Co To Znamená Pro Vás
**Claude Pro má 200,000 token context window = ~150,000 slov = ~500 stran textu**
**Praktický význam pro development:**
- ✅ Upload celého small-to-medium codebase (5-10k LoC)
- ✅ Analyzuj múltiple API responses najednou
- ✅ Code review celého PR v jednom promptu
- ✅ Udržuj konverzaci o projektu celý den bez context loss
**Comparison:**
| Model | Context Window | Praktický Use Case |
|-------|---------------|-------------------|
| Claude 3.5 Sonnet | 200K tokens | ✅ Celý medium codebase |
| GPT-4 Turbo | 128K tokens | ✅ Large files, dokumenty |
| GPT-4 | 8K tokens | ⚠️ Single file review only |
| Gemini 1.5 Pro | 1M tokens | 🚀 Entire large codebase |
---
### 📋 Praktické Použití Extended Context
#### Use Case 1: Complete Codebase Analysis
**Scenario:** Analyzuj architektonické problémy v celém projektu
**Traditional Approach (8K context):**
```
❌ Problem: Musíte rozdělit na 10+ samostatných promptů
❌ Context loss mezi prompty
❌ Inconsistent recommendations
❌ Zabere 2-3 hodiny
```
**Extended Context Approach (200K):**
**Step 1: Export Celého Codebase**
```bash
# Generate project tree + key files
tree -L 3 -I 'node_modules|dist|build' > project-structure.txt
# Combine all source files
find src -name "*.ts" -o -name "*.tsx" | xargs cat > all-source.txt
# Add package.json, configs
cat package.json tsconfig.json >> all-source.txt
# Total: ~50K tokens (well within 200K limit)
```
**Step 2: Single Comprehensive Prompt**
```
Analyze this entire Next.js codebase for architectural issues:
PROJECT STRUCTURE:
[paste project-structure.txt]
ALL SOURCE CODE:
[paste all-source.txt - 50K tokens]
PACKAGE.JSON:
[paste package.json]
Provide comprehensive analysis:
1. **Architecture Issues**
- Circular dependencies
- Poor separation of concerns
- Missing abstractions
- Code duplication
2. **Performance Concerns**
- Bundle size problems
- Unnecessary re-renders
- Inefficient queries
- Missing optimizations
3. **Maintainability**
- Code quality hotspots
- Technical debt areas
- Missing tests
- Documentation gaps
4. **Security**
- Vulnerabilities
- Exposed secrets
- Unsafe patterns
- Missing validations
5. **Refactoring Roadmap**
- Priority order (Critical → Nice-to-have)
- Estimated effort per task
- Dependencies between tasks
- Quick wins vs long-term improvements
Format each finding with:
- File path + line numbers
- Current code snippet
- Problem explanation
- Suggested fix with code
- Impact assessment (🔴 Critical, ⚠️ Warning, ℹ️ Info)
```
**Claude Output (Single Response):**
```markdown
# Codebase Analysis Report
## 🔴 Critical Issues (Fix Immediately)
### 1. SQL Injection Vulnerability
**File:** `src/api/users.ts:45`
**Current Code:**
```typescript
const query = `SELECT * FROM users WHERE email = '${email}'`;
const user = await db.raw(query);
```
**Problem:** Direct string interpolation allows SQL injection
**Fix:**
```typescript
const user = await db('users')
.where('email', email)
.first();
```
**Impact:** 🔴 Critical security vulnerability
---
### 2. Exposed API Key in Client Bundle
**File:** `src/lib/analytics.ts:12`
**Current Code:**
```typescript
const ANALYTICS_KEY = 'sk-live-abc123...'; // ❌
```
**Problem:** Secret key bundled in client-side code
**Fix:**
```typescript
// Move to environment variable
const ANALYTICS_KEY = process.env.NEXT_PUBLIC_ANALYTICS_KEY;
// Add to .env (server-side only)
ANALYTICS_KEY=sk-live-abc123...
// Use Next.js API route for analytics
```
**Impact:** 🔴 Exposed credentials
[... continues with 20+ more issues across entire codebase ...]
```
**Result:**
- ✅ Comprehensive analysis in single prompt
- ✅ Consistent recommendations
- ✅ Prioritized action plan
- ✅ Completed in 5 minutes vs 2-3 hours
---
#### Use Case 2: Multi-File Refactoring Planning
**Scenario:** Refactor authentication system across 10+ files
**Extended Context Approach:**
**Prompt:**
```
Plan refactoring of authentication system across these files:
FILE 1: src/lib/auth.ts (current implementation)
[paste 500 lines]
FILE 2: src/api/auth/login.ts
[paste 200 lines]
FILE 3: src/api/auth/register.ts
[paste 200 lines]
FILE 4: src/middleware/auth.ts
[paste 150 lines]
FILE 5: src/hooks/useAuth.ts
[paste 300 lines]
FILE 6: src/contexts/AuthContext.tsx
[paste 400 lines]
FILE 7: tests/auth.test.ts
[paste 600 lines]
... [8 more files, total ~5K lines = ~40K tokens]
GOAL: Refactor to:
- Separate business logic from UI logic
- Centralize token management
- Improve testability
- Add proper TypeScript types
- Support multiple auth providers (email, Google, GitHub)
Provide:
1. Refactoring roadmap (step-by-step, maintaining working state)
2. For each step:
- Files to modify
- Before/after code for each change
- Tests to update
- Migration strategy
3. Risk assessment per step
4. Rollback plan if something breaks
```
**Claude Output:**
```markdown
# Auth Refactoring Roadmap
## Step 1: Extract Core Auth Logic (1-2 hours, Low Risk ✅)
### Files to Modify:
1. `src/lib/auth/core.ts` (NEW) - Core auth logic
2. `src/lib/auth/types.ts` (NEW) - TypeScript types
3. `src/lib/auth.ts` (REFACTOR) - Use new core
### Implementation:
#### NEW: src/lib/auth/core.ts
```typescript
// Extract pure business logic
export class AuthService {
private tokenManager: TokenManager;
private apiClient: ApiClient;
async login(credentials: LoginCredentials): Promise<AuthResult> {
// Pure logic, no UI dependencies
const { email, password } = credentials;
const tokens = await this.apiClient.post('/auth/login', { email, password });
await this.tokenManager.setTokens(tokens);
return { success: true, user: tokens.user };
}
async logout(): Promise<void> {
await this.tokenManager.clearTokens();
await this.apiClient.post('/auth/logout');
}
// ... more methods
}
```
#### REFACTOR: src/lib/auth.ts
```typescript
// BEFORE: 500 lines of mixed logic
// AFTER: Thin wrapper around core service
import { AuthService } from './auth/core';
const authService = new AuthService();
export const login = (credentials) => authService.login(credentials);
export const logout = () => authService.logout();
// ...
```
### Tests to Update:
- `tests/auth.test.ts` - Add tests for AuthService class
- Mock TokenManager and ApiClient
### Risk: Low ✅
- Existing API unchanged (backwards compatible)
- Can be done incrementally
- Easy to rollback (delete new files, revert refactor)
---
## Step 2: Centralize Token Management (2-3 hours, Medium Risk ⚠️)
[... detailed step-by-step for remaining 5 steps ...]
```
**Result:**
- ✅ Complete refactoring plan across 10+ files
- ✅ Step-by-step with code snippets
- ✅ Risk assessment per step
- ✅ Testable, incremental migration
---
### 🧠 Context Management Strategies
#### Strategy 1: Hierarchical Context Organization
**Problem:** 200K tokens ≠ infinite memory. Priority matters.
**Solution: Information Hierarchy**
```
HIGH PRIORITY (Always Include):
├─ Current task description
├─ Immediate code context (files being modified)
├─ Error messages / logs
└─ Explicit requirements
MEDIUM PRIORITY (Include if space):
├─ Related code (dependencies, callers)
├─ Architecture overview
├─ Test cases
└─ Documentation
LOW PRIORITY (Omit if needed):
├─ Historical context
├─ Alternative approaches discussed
├─ Tangential code
└─ Verbose logs
```
**Example Optimized Prompt:**
```
TASK: Fix authentication bug in login flow
ERROR LOG (Critical):
[paste exact error, stack trace - 500 tokens]
AFFECTED CODE (Critical):
File: src/api/auth/login.ts
[paste login handler - 1K tokens]
File: src/lib/auth.ts
[paste auth helper - 1K tokens]
DEPENDENCIES (Medium Priority):
File: src/middleware/auth.ts
[paste middleware - 800 tokens]
ARCHITECTURE CONTEXT (Medium):
- JWT tokens stored in httpOnly cookies
- Refresh token rotation enabled
- Token expiry: 15min access, 7d refresh
TESTS (Low Priority, omit for now):
[skip to save tokens, add later if needed]
Total Prompt: ~4K tokens (98% headroom remaining)
```
**Result:** Focused, efficient context usage
---
#### Strategy 2: Context Compression Techniques
**Technique 1: Summarization**
**Instead of:**
```
[paste entire 10K line codebase]
Find all instances of XYZ pattern
```
**Do this:**
```
Summarize this codebase structure:
[paste 10K lines]
Focus on:
- File organization
- Key modules and their responsibilities
- External dependencies
- Architecture patterns used
```
**Then use summary:**
```
Based on this architecture:
[paste Claude's 500-token summary]
Now help me [specific task]
[paste only relevant files - 2K tokens]
```
**Savings:** 10K tokens → 2.5K tokens (75% reduction) ✅
---
**Technique 2: Diff-Based Context**
**Instead of:**
```
Review these 10 files (10K LoC):
[paste entire files]
```
**Do this:**
```
Review this refactoring:
FILES CHANGED (summary):
1. src/auth.ts - Extracted core logic
2. src/api/login.ts - Use new auth service
3. ... (8 more files)
GIT DIFF (only changes):
[paste git diff output - 2K tokens, shows only changes]
CONTEXT:
- Goal: Separate business logic from UI
- Pattern: Service layer architecture
```
**Savings:** 10K tokens → 2K tokens (80% reduction) ✅
---
**Technique 3: Abstract-Then-Detail Pattern**
**Instead of:**
```
[paste everything at once - 50K tokens]
Analyze for issues
```
**Do this - Two-Phase Approach:**
**Phase 1: High-Level Analysis**
```
Analyze this project structure:
FILE TREE (500 tokens):
[paste project tree]
PACKAGE.JSON (300 tokens):
[paste dependencies]
README (200 tokens):
[paste project description]
Identify potential architectural issues at high level.
```
**Claude identifies:** "Potential circular dependencies in auth module, missing tests in payment module"
**Phase 2: Deep Dive (Targeted)**
```
You identified circular dependencies in auth module. Here's the detailed code:
[paste ONLY auth module files - 5K tokens]
Analyze the circular dependency:
- Which files are involved?
- What's the cycle?
- How to break it?
```
**Savings:** 50K tokens → 6K tokens (88% reduction) ✅
---
## 2. Claude-Specific Advanced Features
### 🎨 Artifacts (Interactive Code & Documents)
**What Are Artifacts?**
Artifacts are **interactive, editable outputs** that Claude generates in a separate panel. Unlike regular chat text, artifacts can be:
- Edited and re-run
- Exported independently
- Reused across conversations
**Artifact Types:**
1. **Code** (HTML, JavaScript, React, SVG)
2. **Documents** (Markdown, text)
3. **Diagrams** (Mermaid)
---
#### Artifact Use Case 1: Interactive Prototypes
**Prompt:**
```
Create an interactive Next.js login form prototype:
Requirements:
- Email + password fields
- Client-side validation
- Loading state
- Error handling
- Responsive design (Tailwind)
Make it a working prototype I can test in browser.
```
**Claude Response:**
```typescript
// Artifact: LoginForm.tsx (interactive React component)
import { useState } from 'react';
export default function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
setError('');
// Validation
if (!email || !password) {
setError('Please fill in all fields');
return;
}
if (!/\S+@\S+\.\S+/.test(email)) {
setError('Invalid email format');
return;
}
setLoading(true);
// Simulate API call
await new Promise(resolve => setTimeout(resolve, 1500));
setLoading(false);
alert(`Login successful: ${email}`);
};
return (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<div className="bg-white p-8 rounded-lg shadow-md w-96">
<h2 className="text-2xl font-bold mb-6">Login</h2>
<form onSubmit={handleSubmit}>
<div className="mb-4">
<label className="block text-gray-700 mb-2">Email</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="[email protected]"
/>
</div>
<div className="mb-6">
<label className="block text-gray-700 mb-2">Password</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="••••••••"
/>
</div>
{error && (
<div className="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded">
{error}
</div>
)}
<button
type="submit"
disabled={loading}
className="w-full bg-blue-500 text-white py-2 rounded-lg hover:bg-blue-600 disabled:bg-gray-400 disabled:cursor-not-allowed"
>
{loading ? 'Logging in...' : 'Login'}
</button>
</form>
</div>
</div>
);
}
```
**Benefits:**
- ✅ **Interactive Preview:** See form working in real-time in Claude UI
- ✅ **Editable:** Modify code directly in artifact panel
- ✅ **Exportable:** Copy to your project
- ✅ **Iterative:** Request changes, Claude updates artifact
**Iteration Example:**
```
User: "Add a 'Remember Me' checkbox"
Claude: [updates artifact with checkbox added]
User: "Add Google login button"
Claude: [updates artifact again]
```
**Result:** Rapid prototyping loop (seconds per iteration vs minutes copy-pasting)
---
#### Artifact Use Case 2: Architecture Diagrams
**Prompt:**
```
Create architecture diagram for microservices system:
Services:
- API Gateway (Node.js)
- Auth Service (Python)
- Product Service (Node.js)
- Order Service (Java)
- Payment Service (Node.js)
- Notification Service (Python)
Infrastructure:
- PostgreSQL (Auth, Product, Order)
- Redis (caching, sessions)
- RabbitMQ (async messaging)
- Elasticsearch (search)
Show:
- Service communication (REST, RabbitMQ)
- Data flows
- External integrations (Stripe, SendGrid)
```
**Claude Response (Mermaid Diagram Artifact):**
```mermaid
graph TD
Client[Client Apps]
Gateway[API Gateway<br/>Node.js<br/>:8000]
Auth[Auth Service<br/>Python<br/>:8001]
Product[Product Service<br/>Node.js<br/>:8002]
Order[Order Service<br/>Java<br/>:8003]
Payment[Payment Service<br/>Node.js<br/>:8004]
Notif[Notification Service<br/>Python<br/>:8005]
AuthDB[(PostgreSQL<br/>Auth DB)]
ProductDB[(PostgreSQL<br/>Product DB)]
OrderDB[(PostgreSQL<br/>Order DB)]
Redis[(Redis<br/>Cache/Session)]
Queue[RabbitMQ<br/>Message Queue]
Search[(Elasticsearch<br/>Product Search)]
Stripe[Stripe API]
SendGrid[SendGrid API]
Client -->|REST| Gateway
Gateway -->|REST| Auth
Gateway -->|REST| Product
Gateway -->|REST| Order
Gateway -->|REST| Payment
Auth --> AuthDB
Product --> ProductDB
Order --> OrderDB
Auth --> Redis
Gateway --> Redis
Product --> Search
Order -->|order.created| Queue
Payment -->|payment.processed| Queue
Queue -->|consume| Notif
Payment -->|process payment| Stripe
Notif -->|send email| SendGrid
style Gateway fill:#4CAF50
style Auth fill:#2196F3
style Product fill:#2196F3
style Order fill:#FF9800
style Payment fill:#2196F3
style Notif fill:#9C27B0
```
**Benefits:**
- ✅ **Visual:** Immediate visual representation
- ✅ **Editable:** Modify diagram by editing Mermaid code
- ✅ **Exportable:** PNG, SVG, or Mermaid code
- ✅ **Collaborative:** Share with team for architecture discussions
**Iteration:**
```
User: "Add load balancer before API Gateway"
Claude: [updates diagram with LB]
User: "Show database replication"
Claude: [adds replica databases]
```
---
### 🔧 Analysis Tool (Data Processing & Code Execution)
**What Is It?**
Claude's built-in **Python execution environment** for:
- Data analysis
- File processing
- Calculations
- Code testing
**NOT the same as:** ChatGPT's Code Interpreter (different implementation, different capabilities)
---
#### Analysis Tool Use Case: Large Dataset Processing
**Scenario:** Analyze CSV with API request logs (100K+ rows)
**Prompt:**
```
Analyze this API logs CSV:
[upload api-logs.csv - 5MB file, 100K rows]
Questions:
1. What's the average response time per endpoint?
2. Which endpoints have highest error rates?
3. Identify performance anomalies (>2s response time)
4. Traffic patterns by hour of day
5. Top 10 slowest requests with details
Provide:
- Summary statistics
- Visualizations (charts)
- Specific slow requests to investigate
- Recommendations
```
**Claude Response (Using Analysis Tool):**
```python
# Claude runs this Python code internally:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load data
df = pd.read_csv('api-logs.csv')
# 1. Average response time per endpoint
avg_response = df.groupby('endpoint')['response_time_ms'].mean().sort_values(ascending=False)
# 2. Error rates
error_rate = df.groupby('endpoint').apply(
lambda x: (x['status'].str.startswith('5').sum() / len(x)) * 100
).sort_values(ascending=False)
# 3. Performance anomalies
slow_requests = df[df['response_time_ms'] > 2000]
# 4. Traffic by hour
df['hour'] = pd.to_datetime(df['timestamp']).dt.hour
traffic_by_hour = df.groupby('hour').size()
# 5. Top 10 slowest
top_slowest = df.nlargest(10, 'response_time_ms')[[
'endpoint', 'method', 'response_time_ms', 'status', 'timestamp'
]]
# Visualizations
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
# Plot 1: Avg response time
avg_response.head(10).plot(kind='barh', ax=axes[0,0])
axes[0,0].set_title('Top 10 Slowest Endpoints (Avg)')
axes[0,0].set_xlabel('Avg Response Time (ms)')
# Plot 2: Error rates
error_rate.head(10).plot(kind='barh', ax=axes[0,1], color='red')
axes[0,1].set_title('Top 10 Error Rates (%)')
# Plot 3: Traffic by hour
traffic_by_hour.plot(kind='line', ax=axes[1,0], marker='o')
axes[1,0].set_title('Traffic Patterns by Hour')
axes[1,0].set_xlabel('Hour of Day')
axes[1,0].set_ylabel('Request Count')
# Plot 4: Response time distribution
df['response_time_ms'].plot(kind='hist', bins=50, ax=axes[1,1])
axes[1,1].set_title('Response Time Distribution')
axes[1,1].set_xlabel('Response Time (ms)')
plt.tight_layout()
plt.show()
# Output results
```
**Claude Returns:**
```markdown
## API Logs Analysis Results
### 1. Average Response Time by Endpoint
| Endpoint | Avg Response Time (ms) |
|----------|----------------------|
| POST /api/reports/generate | 3,247 ms | 🔴
| GET /api/analytics/dashboard | 1,832 ms | ⚠️
| POST /api/exports/csv | 1,654 ms | ⚠️
| GET /api/users/search | 421 ms | ✅
| POST /api/auth/login | 234 ms | ✅
**Critical Issues:**
- 🔴 `/api/reports/generate` is 3.2s on average (way too slow!)
- ⚠️ Analytics dashboard taking 1.8s (should be <500ms for UX)
### 2. Error Rates
| Endpoint | Error Rate |
|----------|-----------|
| POST /api/payments/process | 12.3% | 🔴
| GET /api/external/data-sync | 8.7% | 🔴
| POST /api/webhooks/stripe | 5.4% | ⚠️
| GET /api/products/list | 1.2% | ✅
**Critical Issues:**
- 🔴 Payment processing failing 12% of the time!
- 🔴 External data sync unreliable
### 3. Performance Anomalies (>2s)
**Found 1,247 slow requests (1.2% of total)**
**Common patterns:**
- 847 requests (68%) are report generation (expected, heavy operation)
- 234 requests (19%) are search queries with complex filters
- 166 requests (13%) are timeouts waiting for external APIs
**Unexpected:**
- 23 requests to simple endpoints (e.g., `/api/users/:id`) took >2s
→ Investigate: Database connection pool exhaustion?
### 4. Traffic Patterns
**Peak hours:**
- 9-11 AM: 15,234 req/hr (morning report generation)
- 2-4 PM: 12,456 req/hr (afternoon activity)
- 8-10 PM: 8,123 req/hr (evening usage)
**Low hours:**
- 12-6 AM: <1,000 req/hr
**Insight:** Consider scaling up during 9-11 AM peak
### 5. Top 10 Slowest Requests
| Endpoint | Method | Response Time | Status | Timestamp |
|----------|--------|--------------|--------|-----------|
| /api/reports/generate | POST | 45,234 ms | 200 | 2024-01-15 09:23:45 |
| /api/reports/generate | POST | 38,567 ms | 200 | 2024-01-15 09:45:12 |
| /api/analytics/dashboard | GET | 28,345 ms | 504 | 2024-01-16 14:23:11 |
| /api/exports/csv | POST | 25,123 ms | 200 | 2024-01-15 10:12:34 |
| ... | ... | ... | ... | ... |
**Critical Finding:**
- Report generation taking up to 45 seconds (timeout = 30s, some fail!)
- Analytics dashboard timing out (504 errors)
## 📊 Visualizations
[Claude shows 4 charts as described in Python code]
## 🎯 Recommendations
### High Priority 🔴
1. **Fix Payment Processing (12% error rate)**
- Investigate Stripe API integration
- Add retry logic
- Implement idempotency
- Alert on failures
2. **Optimize Report Generation (45s response times)**
- Move to background job queue (BullMQ, Celery)
- Return immediately, email when done
- Add progress tracking
- Implement caching for common reports
3. **Scale Analytics Dashboard (1.8s avg, 28s max)**
- Pre-aggregate data hourly
- Use materialized views
- Implement Redis caching
- Add pagination (don't load everything at once)
### Medium Priority ⚠️
4. **External API Reliability (8.7% error rate)**
- Add circuit breaker pattern
- Implement exponential backoff
- Cache responses when possible
- Add fallback data
5. **Database Connection Pool**
- Investigate simple endpoint slowness (23 cases >2s)
- Increase pool size if needed
- Add connection monitoring
### Long-Term Improvements ℹ️
6. **Auto-Scaling Based on Traffic**
- Scale up at 8 AM (before 9 AM peak)
- Scale down at 11 PM
- Save ~40% infrastructure cost
7. **Performance Monitoring**
- Set up real-time alerts (>2s response, >5% errors)
- APM tool (DataDog, New Relic)
- Track performance trends
---
**Next Steps:**
1. Create tickets for High Priority items
2. Investigate payment processing logs (detailed error analysis)
3. Design background job system for reports
4. Set up monitoring/alerting
```
**Benefits:**
- ✅ **Fast Analysis:** 100K rows processed in seconds
- ✅ **Visual Insights:** Charts show patterns immediately
- ✅ **Actionable:** Specific recommendations with priorities
- ✅ **No Code Required:** Claude writes Python, you get results
---
### 📝 Projects Feature (Organize Long-Term Work)
**What Are Projects?**
Projects in Claude are **persistent workspaces** with:
- Custom instructions
- Shared knowledge base
- Conversation history
- Context that persists across chats
**Think:** Like a dedicated Claude instance for each project
---
#### Project Setup Example
**Project: "NextJS E-commerce App"**
**Custom Instructions:**
```
You're helping build a Next.js 14 e-commerce application.
TECH STACK:
- Next.js 14 (App Router)
- TypeScript (strict mode)
- PostgreSQL + Prisma
- Tailwind CSS
- tRPC for API
- Stripe for payments
- NextAuth for authentication
CODING STANDARDS:
- Use server components by default
- Client components only when needed ('use client')
- Prefer server actions over API routes
- TypeScript strict mode (no 'any')
- ESLint + Prettier
- 80%+ test coverage target
ARCHITECTURE:
- Repository pattern for data access
- Service layer for business logic
- Separate API types from database types
- Server actions in app/actions/
- Services in lib/services/
- Database in lib/db/
WHEN GIVING CODE:
- Always include TypeScript types
- Include error handling
- Add basic tests
- Consider performance (n+1 queries, etc.)
- Security (input validation, SQL injection, XSS)
PROJECT CONTEXT:
- Current phase: MVP development
- Priority: Core e-commerce features (cart, checkout, orders)
- Timeline: Launching in 6 weeks
```
**Knowledge Base (Upload Files):**
- `architecture.md` - System design doc
- `api-spec.md` - API contracts
- `database-schema.md` - Prisma schema
- `coding-standards.md` - Team conventions
---
**Usage in Conversations:**
**Chat 1 (Product Implementation):**
```
User: "Implement product catalog with search and filters"
Claude: [References custom instructions automatically]
I'll implement this following your architecture:
- Server component for product list (app/products/page.tsx)
- Server action for filters (app/actions/products.ts)
- Service layer (lib/services/productService.ts)
- Repository (lib/db/repositories/productRepository.ts)
[Provides implementation following project standards]
```
**Chat 2 (Days Later, Different Feature):**
```
User: "Add shopping cart functionality"
Claude: [Remembers project context from Project knowledge base]
Based on your existing architecture (from architecture.md):
- I'll use the same repository pattern
- Follow the auth flow you established
- Integrate with your existing product service
[Consistent with previous work, no need to re-explain project structure]
```
**Benefits:**
- ✅ **Consistency:** Claude remembers project conventions
- ✅ **No Repetition:** Don't re-explain tech stack every chat
- ✅ **Faster:** Jump straight to work
- ✅ **Team Sharing:** Share project with team members
---
## 3. Tool Use & Function Calling
### 🛠️ What Is Tool Use?
**Tool Use** = AI model calling external functions/APIs to complete tasks
**How It Works:**
1. You define available tools (functions with descriptions)
2. AI decides when to call which tool
3. AI constructs proper function call with parameters
4. Your code executes the function
5. AI uses result to continue task
**Why It Matters:**
- ✅ AI can access real-time data (stock prices, weather, databases)
- ✅ AI can perform actions (send emails, create tickets, deploy code)
- ✅ AI can integrate with your systems (internal APIs, tools)
---
### 📋 Practical Implementation Example
#### Scenario: AI-Powered Customer Support Bot
**Tools Available:**
1. `get_customer_info(email)` - Fetch customer data from database
2. `get_order_status(order_id)` - Check order status
3. `create_support_ticket(customer_email, issue, priority)` - Create ticket in system
4. `send_email(to, subject, body)` - Send email
**Implementation (Claude API + Python):**
```python
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-...")
# Define available tools
tools = [
{
"name": "get_customer_info",
"description": "Retrieves customer information from database by email address. Returns customer name, account status, subscription tier, and join date.",
"input_schema": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "Customer email address"
}
},
"required": ["email"]
}
},
{
"name": "get_order_status",
"description": "Fetches current status of an order by order ID. Returns order status (processing/shipped/delivered), tracking number if available, and estimated delivery date.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "Unique order identifier (format: ORD-XXXXX)"
}
},
"required": ["order_id"]
}
},
{
"name": "create_support_ticket",
"description": "Creates a support ticket in the system. Use when customer issue requires human agent intervention.",
"input_schema": {
"type": "object",
"properties": {
"customer_email": {"type": "string"},
"issue": {
"type": "string",
"description": "Detailed description of the customer issue"
},
"priority": {
"type": "string",
"enum": ["low", "medium", "high", "urgent"],
"description": "Priority level based on issue severity"
}
},
"required": ["customer_email", "issue", "priority"]
}
},
{
"name": "send_email",
"description": "Sends email to customer. Use for order confirmations, shipping updates, or support responses.",
"input_schema": {
"type": "object",
"properties": {
"to": {"type": "string"},
"subject": {"type": "string"},
"body": {"type": "string"}
},
"required": ["to", "subject", "body"]
}
}
]
# Actual tool implementations
def get_customer_info(email):
# Query database
return {
"name": "John Doe",
"email": email,
"status": "active",
"tier": "premium",
"join_date": "2023-05-12"
}
def get_order_status(order_id):
# Query order system
return {
"order_id": order_id,
"status": "shipped",
"tracking": "1Z999AA10123456784",
"estimated_delivery": "2024-01-20"
}
def create_support_ticket(customer_email, issue, priority):
# Create ticket in support system
ticket_id = "TICKET-12345"
return {"ticket_id": ticket_id, "status": "created"}
def send_email(to, subject, body):
# Send via email service
return {"sent": True, "message_id": "msg-xyz"}
# Tool router
def execute_tool(tool_name, tool_input):
if tool_name == "get_customer_info":
return get_customer_info(**tool_input)
elif tool_name == "get_order_status":
return get_order_status(**tool_input)
elif tool_name == "create_support_ticket":
return create_support_ticket(**tool_input)
elif tool_name == "send_email":
return send_email(**tool_input)
# Customer support conversation
def handle_customer_message(user_message):
messages = [{"role": "user", "content": user_message}]
while True:
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
tools=tools,
messages=messages
)
# Check if Claude wants to use a tool
if response.stop_reason == "tool_use":
# Claude decided to call a tool
tool_use_block = next(
block for block in response.content
if block.type == "tool_use"
)
tool_name = tool_use_block.name
tool_input = tool_use_block.input
print(f"🔧 Claude calling tool: {tool_name}")
print(f" Parameters: {tool_input}")
# Execute the tool
tool_result = execute_tool(tool_name, tool_input)
# Send result back to Claude
messages.append({"role": "assistant", "content": response.content})
messages.append({
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": tool_use_block.id,
"content": str(tool_result)
}]
})
# Continue conversation (Claude might call more tools)
continue
else:
# Claude has final response for user
final_response = next(
block.text for block in response.content
if hasattr(block, "text")
)
return final_response
# Example usage
customer_query = """
Hi, I'm [email protected] and I'm checking on my order ORD-12345.
Can you tell me where it is? Also, I need to upgrade my account.
"""
response = handle_customer_message(customer_query)
print(response)
```
**Execution Flow:**
```
User: "Hi, I'm [email protected] and I'm checking on my order ORD-12345. Can you tell me where it is? Also, I need to upgrade my account."
🔧 Claude calling tool: get_customer_info
Parameters: {'email': '[email protected]'}
[Tool returns: {name: "John Doe", tier: "premium", ...}]
🔧 Claude calling tool: get_order_status
Parameters: {'order_id': 'ORD-12345'}
[Tool returns: {status: "shipped", tracking: "1Z999AA10123456784", ...}]
🔧 Claude calling tool: create_support_ticket
Parameters: {
'customer_email': '[email protected]',
'issue': 'Customer wants to upgrade account from premium tier',
'priority': 'medium'
}
[Tool returns: {ticket_id: "TICKET-12345"}]
Claude's Response:
"Hi John! I've checked on your order ORD-12345. Good news - it has been shipped! Your tracking number is 1Z999AA10123456784, and it's estimated to arrive on January 20th.
Regarding your account upgrade: I see you're currently on our premium tier. I've created a support ticket (TICKET-12345) for our billing team to contact you about upgrade options to our enterprise tier. They'll reach out within 24 hours to discuss pricing and features.
Is there anything else I can help you with today?"
```
**Benefits:**
- ✅ **Autonomous:** Claude decides which tools to call
- ✅ **Multi-Step:** Claude can call multiple tools in sequence
- ✅ **Contextual:** Claude uses tool results intelligently
- ✅ **Natural:** User doesn't see technical tool calls
---
### 🚀 Advanced Tool Use Patterns
#### Pattern 1: Sequential Tool Chains
**Scenario:** Deploy code to production
**Tools:**
1. `run_tests()` - Run test suite
2. `build_project()` - Build production bundle
3. `deploy_to_server(version)` - Deploy to production
4. `send_slack_notification(message)` - Notify team
**User Request:** "Deploy the latest changes to production"
**Claude's Execution:**
```
🔧 Call: run_tests()
Result: {passed: 47, failed: 0, coverage: 87%}
✅ Tests passed!
🔧 Call: build_project()
Result: {success: true, bundle_size: "2.3 MB", build_time: "45s"}
✅ Build successful!
🔧 Call: deploy_to_server(version="v1.2.3")
Result: {deployed: true, url: "https://app.example.com", version: "v1.2.3"}
✅ Deployed!
🔧 Call: send_slack_notification(
message="🚀 Deployed v1.2.3 to production. Tests: 47/47 passed. Bundle: 2.3MB"
)
Final Response: "Deployment successful! I've completed the following steps:
1. ✅ Ran all tests (47/47 passed, 87% coverage)
2. ✅ Built production bundle (2.3 MB)
3. ✅ Deployed version v1.2.3 to https://app.example.com
4. ✅ Notified team on Slack
Your changes are now live in production!"
```
**Result:** Multi-step deployment automated ✅
---
#### Pattern 2: Conditional Tool Use
**Scenario:** Smart error handling
**User:** "Check server health"
**Claude's Logic:**
```
🔧 Call: check_server_status()
Result: {status: "unhealthy", error_rate: 15%, response_time: 3500ms}
⚠️ Server unhealthy!
🔧 Call: get_error_logs(limit=10)
Result: [... 10 recent errors ...]
🔧 Call: analyze_errors(logs)
Result: {
root_cause: "Database connection pool exhausted",
affected_endpoints: ["/api/reports", "/api/analytics"]
}
🔧 Call: create_incident(
severity="high",
title="Database connection pool exhausted",
details="..."
)
🔧 Call: send_alert_to_oncall(incident_id="INC-123")
Final Response: "⚠️ Server health issue detected:
**Problem:** Database connection pool exhausted
**Impact:** 15% error rate, 3.5s avg response time
**Affected Endpoints:**
- /api/reports
- /api/analytics
**Actions Taken:**
✅ Created incident INC-123 (high severity)
✅ Alerted on-call engineer
**Immediate Recommendation:**
Increase database connection pool size from 10 to 25 connections.
```
**Result:** Intelligent triage + automated response ✅
---
## 4. Streaming & Real-Time Processing
### 🌊 What Is Streaming?
**Traditional (Non-Streaming):**
```
User: "Write a 1000-word article"
[... waits 30 seconds ...]
Claude: [entire 1000-word article appears at once]
```
**Streaming:**
```
User: "Write a 1000-word article"
Claude: "The future of AI is..." [words appear as they're generated]
```
**Why Streaming Matters:**
- ✅ **Perceived Performance:** User sees progress immediately
- ✅ **Better UX:** Can read while AI is still writing
- ✅ **Interruptible:** Can stop generation early if not relevant
- ✅ **Real-Time Feedback:** See AI's reasoning in real-time
---
### 📋 Streaming Implementation (Claude API)
**Python Example:**
```python
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-...")
# Non-streaming (traditional)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short story"}]
)
print(response.content[0].text) # Waits for complete response
# Streaming (real-time)
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short story"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True) # Print each word as it arrives
```
**Output:**
```
Once upon a time... [appears]
in a small village... [appears]
there lived a brave... [appears]
young programmer... [appears]
... [continues in real-time] ...
```
---
### 🚀 Advanced Streaming: Real-Time Code Generation
**Scenario:** Generate large codebase in real-time
**Implementation:**
```python
import anthropic
import os
client = anthropic.Anthropic()
def generate_codebase_streaming(project_description):
prompt = f"""Generate a complete Next.js project based on this description:
{project_description}
Provide:
1. File structure
2. All source files with complete code
3. package.json
4. README.md
Format each file as:
--- FILE: path/to/file.ts ---
[code]
---
"""
current_file = None
current_content = []
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=8000,
messages=[{"role": "user", "content": prompt}]
) as stream:
for text in stream.text_stream:
# Parse streaming output for file markers
if "--- FILE:" in text:
# Save previous file
if current_file:
save_file(current_file, "".join(current_content))
print(f"✅ Generated: {current_file}")
# Start new file
current_file = text.split("FILE:")[1].split("---")[0].strip()
current_content = []
elif "---" in text and current_file:
# End of file
save_file(current_file, "".join(current_content))
print(f"✅ Generated: {current_file}")
current_file = None
current_content = []
elif current_file:
# Accumulate file content
current_content.append(text)
print(text, end="", flush=True) # Show progress
def save_file(filepath, content):
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w') as f:
f.write(content)
# Usage
project_desc = """
E-commerce product catalog with:
- Product listing with search/filter
- Product detail pages
- Shopping cart
- TypeScript + Next.js 14 + Tailwind
"""
generate_codebase_streaming(project_desc)
```
**Output (Real-Time):**
```
Generating files...
--- FILE: package.json ---
{
"name": "ecommerce-catalog",
"version": "1.0.0",
... [streaming content] ...
}
✅ Generated: package.json
--- FILE: app/page.tsx ---
import { ProductList } from './components/ProductList';
export default function Home() {
... [streaming content] ...
}
✅ Generated: app/page.tsx
--- FILE: app/components/ProductList.tsx ---
'use client';
import { useState, useEffect } from 'react';
... [streaming content] ...
✅ Generated: app/components/ProductList.tsx
[... continues for all files ...]
✅ Complete project generated!
```
**Benefits:**
- ✅ **Progress Visibility:** See each file being generated
- ✅ **Faster Perceived Performance:** Don't wait for entire codebase
- ✅ **Interruptible:** Can stop if going wrong direction
- ✅ **File-by-File Processing:** Save files as they complete
---
## 5. Advanced Prompt Engineering Techniques
### 🎯 System Instructions (Custom Behavior)
**What:** Define persistent AI behavior across all conversations
**Example: Code Review Bot**
```python
system_instructions = """You are a senior software engineer conducting code reviews.
TONE & STYLE:
- Be constructive, not critical
- Explain WHY issues matter (security, performance, maintainability)
- Provide code examples for fixes
- Use emojis for severity: 🔴 Critical, ⚠️ Warning, ℹ️ Info, ✅ Good
REVIEW CHECKLIST:
1. Security (SQL injection, XSS, secrets exposure, auth issues)
2. Performance (n+1 queries, unnecessary re-renders, bundle size)
3. Code Quality (DRY, SOLID, naming, complexity)
4. Testing (missing tests, edge cases, coverage)
5. Best Practices (framework conventions, modern patterns)
OUTPUT FORMAT:
## 🔴 Critical Issues (Fix Now)
[List with file:line, code snippet, fix]
## ⚠️ Warnings (Should Fix)
[...]
## ℹ️ Suggestions (Nice to Have)
[...]
## ✅ Good Practices
[Highlight what's done well]
Always end with estimated time to fix all issues.
"""
# Use in API call
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
system=system_instructions, # <-- Custom behavior
messages=[{
"role": "user",
"content": "Review this code: [paste code]"
}]
)
```
**Result:** Consistent, structured code reviews every time ✅
---
### 🔗 Chain-of-Thought Prompting
**What:** Ask AI to "think step-by-step" before answering
**Example: Complex Algorithm Design**
**Without CoT:**
```
User: "Design algorithm for finding duplicate transactions in large dataset"
Claude: "Here's the algorithm: [jumps to solution, may miss edge cases]"
```
**With CoT:**
```
User: """Design algorithm for finding duplicate transactions in large dataset.
Think through this step-by-step:
1. What defines a "duplicate" transaction?
2. What edge cases exist?
3. What are performance constraints?
4. What data structures would be most efficient?
5. How to handle false positives?
Then provide the final algorithm."""
Claude:
"Let me think through this systematically:
**Step 1: Defining Duplicates**
A duplicate transaction likely means:
- Same amount
- Same timestamp (within a threshold, say ±1 second)
- Same merchant/description
- Same account
But NOT:
- Recurring subscriptions (same amount daily/monthly)
- Split payments (same amount, different merchants)
**Step 2: Edge Cases**
- Clock skew (transactions at 23:59:59 and 00:00:01)
- Partial refunds (same amount, reversed)
- Currency conversion (1.00 USD ≠ 0.85 EUR but might be same transaction)
- Pending vs settled (same transaction appears twice)
**Step 3: Performance Constraints**
- Dataset size: millions of transactions
- Need O(n log n) or better (O(n²) too slow)
- Memory: can't load all in RAM
- Real-time: process as transactions stream in
**Step 4: Data Structures**
Best approach: Hash map with composite key
- Key: (amount, merchant, account, timestamp_bucket)
- Timestamp bucket: round to nearest 5 seconds
- Collisions indicate potential duplicates
Alternative: Sliding window with sorted list
- Sort by timestamp
- For each transaction, check window of ±30 seconds
- Compare attributes
**Step 5: False Positives**
- Two legitimate transactions at same store
- User might buy same item twice
- Solution: Flag for review rather than auto-delete
**Final Algorithm:**
```python
from collections import defaultdict
from datetime import datetime, timedelta
def find_duplicate_transactions(transactions):
# Hash map: composite key → list of transactions
potential_duplicates = defaultdict(list)
confirmed_duplicates = []
for tx in transactions:
# Create composite key (bucket timestamp to 5-second intervals)
timestamp_bucket = (tx.timestamp // 5) * 5
key = (
round(tx.amount, 2),
tx.merchant,
tx.account,
timestamp_bucket
)
potential_duplicates[key].append(tx)
# Review buckets with multiple transactions
for key, tx_list in potential_duplicates.items():
if len(tx_list) > 1:
# Further validation
for i in range(len(tx_list)):
for j in range(i + 1, len(tx_list)):
if are_duplicates(tx_list[i], tx_list[j]):
confirmed_duplicates.append((tx_list[i], tx_list[j]))
return confirmed_duplicates
def are_duplicates(tx1, tx2):
# Fine-grained duplicate check
time_diff = abs(tx1.timestamp - tx2.timestamp)
return (
abs(tx1.amount - tx2.amount) < 0.01 # Same amount (± 1 cent)
and time_diff < 30 # Within 30 seconds
and tx1.merchant == tx2.merchant
and tx1.account == tx2.account
and tx1.status == tx2.status # Both pending or both settled
)
```
**Complexity:**
- Time: O(n) average (O(n log n) if sorting)
- Space: O(n) for hash map
**Handles Edge Cases:**
✅ Clock skew (30-second window)
✅ Currency precision (round to 2 decimals)
✅ Pending vs settled (status check)
✅ False positives (two-level check: bucket + detailed)
**Performance:**
- Can process 1M transactions in ~2-3 seconds
- Memory efficient (hash map stores keys, not full objects)
- Streaming compatible (process in batches)"
```
**Benefits of CoT:**
- ✅ **Better Reasoning:** AI thinks through problem
- ✅ **Fewer Errors:** Catches edge cases
- ✅ **Transparent:** Can see AI's logic
- ✅ **More Accurate:** Final solution is better
---
## 6. Cost Optimization Strategies
### 💰 Token Usage Optimization
**Problem:** API calls are expensive at scale
**Cost Comparison (Claude 3.5 Sonnet):**
- Input: $3 per million tokens
- Output: $15 per million tokens
**Example Costs:**
- 10K input + 2K output = $0.06 per call
- 100 calls/day = $6/day = $180/month
- 1000 calls/day = $60/day = $1,800/month
**Optimization Strategies:**
#### Strategy 1: Prompt Compression
**Inefficient Prompt (10K tokens):**
```
Review this entire codebase:
FILE 1: app/page.tsx (1000 lines)
[paste entire file]
FILE 2: app/layout.tsx (800 lines)
[paste entire file]
... [8 more files, 8000 lines total]
Find security issues.
```
**Optimized Prompt (2K tokens, 80% savings):**
```
Review for security issues:
Security-sensitive files only:
1. src/api/auth.ts (auth logic - 200 lines)
[paste]
2. src/middleware/auth.ts (token validation - 150 lines)
[paste]
3. src/api/users.ts (user operations - 300 lines)
[paste]
Focus: SQL injection, XSS, exposed secrets, insecure auth
```
**Savings:** $0.06 → $0.012 per call (80% cheaper) ✅
---
#### Strategy 2: Caching Results
**Pattern:** Cache AI responses for common queries
```python
import redis
import hashlib
import json
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def get_cached_or_generate(prompt, system_instructions=None):
# Create cache key from prompt
cache_key = hashlib.sha256(
(prompt + str(system_instructions)).encode()
).hexdigest()
# Check cache
cached = redis_client.get(cache_key)
if cached:
print("✅ Cache hit!")
return json.loads(cached)
# Generate (cache miss)
print("❌ Cache miss, calling Claude...")
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
system=system_instructions,
messages=[{"role": "user", "content": prompt}]
)
result = response.content[0].text
# Cache for 24 hours
redis_client.setex(
cache_key,
86400, # 24 hours
json.dumps(result)
)
return result
# Usage
code_review = get_cached_or_generate(
prompt="Review this code: [...]",
system_instructions=code_review_system
)
```
**Savings Example:**
- Same code reviewed 10x/day
- Without cache: 10 API calls/day = $0.60/day
- With cache: 1 API call/day + 9 cache hits = $0.06/day
- **Savings: 90%** ✅
---
#### Strategy 3: Batch Processing
**Inefficient (Multiple Calls):**
```python
# Review 100 files = 100 API calls
for file in files:
review = client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": f"Review {file}"}]
)
# Cost: 100 × $0.06 = $6.00
```
**Optimized (Batching):**
```python
# Batch 10 files per call = 10 API calls
batch_size = 10
for i in range(0, len(files), batch_size):
batch = files[i:i+batch_size]
combined_prompt = "Review these files:
" + "
---
".join(batch)
review = client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": combined_prompt}]
)
# Cost: 10 × $0.15 = $1.50
```
**Savings:** $6.00 → $1.50 (75% cheaper) ✅
---
## Závěr & Action Plan
### ✅ Immediate Next Steps
**Week 1: Learn Extended Context**
1. Try uploading entire codebase to Claude
2. Practice multi-file analysis
3. Use context compression techniques
**Week 2: Master Artifacts**
1. Generate interactive prototypes
2. Create architecture diagrams
3. Use Analysis Tool for data processing
**Week 3: Implement Tool Use**
1. Build simple tool integration (1-2 tools)
2. Test sequential tool chains
3. Add error handling
**Week 4: Optimize Costs**
1. Implement caching
2. Practice prompt compression
3. Batch similar requests
### 📊 Success Metrics
Track these weekly:
- **Token Usage:** Aim for <50K tokens/day
- **Cost:** Target <$5/day for development work
- **Cache Hit Rate:** >60% for repeated queries
- **Time Savings:** Document hours saved vs manual work
### 🚀 Long-Term Mastery (3 Months)
**Month 1:**
- Master extended context + artifacts
- Build first tool integration
- Optimize 50% of prompts
**Month 2:**
- Advanced tool use (3-5 tools)
- Streaming implementation
- Caching system in production
**Month 3:**
- Custom Claude Code workflows
- Full CI/CD integration
- Team rollout & training
**Estimated ROI:**
- Time saved: 40-60% on development tasks
- Quality improvement: Fewer bugs, better architecture
- Cost: $20-40/month for Claude Pro + API usage
- Value: $2,000-4,000/month (based on 60h saved @ $50/hr)
---
**Good luck mastering advanced AI features! Remember: Advanced features are force multipliers - they amplify good practices but also amplify bad ones. Start simple, measure results, iterate. 🚀**
## Maximální Využití Pokročilých Funkcí 🚀
---
## 1. Extended Context Management (200K Tokens)
### 🎯 Co To Znamená Pro Vás
**Claude Pro má 200,000 token context window = ~150,000 slov = ~500 stran textu**
**Praktický význam pro development:**
- ✅ Upload celého small-to-medium codebase (5-10k LoC)
- ✅ Analyzuj múltiple API responses najednou
- ✅ Code review celého PR v jednom promptu
- ✅ Udržuj konverzaci o projektu celý den bez context loss
**Comparison:**
| Model | Context Window | Praktický Use Case |
|-------|---------------|-------------------|
| Claude 3.5 Sonnet | 200K tokens | ✅ Celý medium codebase |
| GPT-4 Turbo | 128K tokens | ✅ Large files, dokumenty |
| GPT-4 | 8K tokens | ⚠️ Single file review only |
| Gemini 1.5 Pro | 1M tokens | 🚀 Entire large codebase |
---
### 📋 Praktické Použití Extended Context
#### Use Case 1: Complete Codebase Analysis
**Scenario:** Analyzuj architektonické problémy v celém projektu
**Traditional Approach (8K context):**
```
❌ Problem: Musíte rozdělit na 10+ samostatných promptů
❌ Context loss mezi prompty
❌ Inconsistent recommendations
❌ Zabere 2-3 hodiny
```
**Extended Context Approach (200K):**
**Step 1: Export Celého Codebase**
```bash
# Generate project tree + key files
tree -L 3 -I 'node_modules|dist|build' > project-structure.txt
# Combine all source files
find src -name "*.ts" -o -name "*.tsx" | xargs cat > all-source.txt
# Add package.json, configs
cat package.json tsconfig.json >> all-source.txt
# Total: ~50K tokens (well within 200K limit)
```
**Step 2: Single Comprehensive Prompt**
```
Analyze this entire Next.js codebase for architectural issues:
PROJECT STRUCTURE:
[paste project-structure.txt]
ALL SOURCE CODE:
[paste all-source.txt - 50K tokens]
PACKAGE.JSON:
[paste package.json]
Provide comprehensive analysis:
1. **Architecture Issues**
- Circular dependencies
- Poor separation of concerns
- Missing abstractions
- Code duplication
2. **Performance Concerns**
- Bundle size problems
- Unnecessary re-renders
- Inefficient queries
- Missing optimizations
3. **Maintainability**
- Code quality hotspots
- Technical debt areas
- Missing tests
- Documentation gaps
4. **Security**
- Vulnerabilities
- Exposed secrets
- Unsafe patterns
- Missing validations
5. **Refactoring Roadmap**
- Priority order (Critical → Nice-to-have)
- Estimated effort per task
- Dependencies between tasks
- Quick wins vs long-term improvements
Format each finding with:
- File path + line numbers
- Current code snippet
- Problem explanation
- Suggested fix with code
- Impact assessment (🔴 Critical, ⚠️ Warning, ℹ️ Info)
```
**Claude Output (Single Response):**
```markdown
# Codebase Analysis Report
## 🔴 Critical Issues (Fix Immediately)
### 1. SQL Injection Vulnerability
**File:** `src/api/users.ts:45`
**Current Code:**
```typescript
const query = `SELECT * FROM users WHERE email = '${email}'`;
const user = await db.raw(query);
```
**Problem:** Direct string interpolation allows SQL injection
**Fix:**
```typescript
const user = await db('users')
.where('email', email)
.first();
```
**Impact:** 🔴 Critical security vulnerability
---
### 2. Exposed API Key in Client Bundle
**File:** `src/lib/analytics.ts:12`
**Current Code:**
```typescript
const ANALYTICS_KEY = 'sk-live-abc123...'; // ❌
```
**Problem:** Secret key bundled in client-side code
**Fix:**
```typescript
// Move to environment variable
const ANALYTICS_KEY = process.env.NEXT_PUBLIC_ANALYTICS_KEY;
// Add to .env (server-side only)
ANALYTICS_KEY=sk-live-abc123...
// Use Next.js API route for analytics
```
**Impact:** 🔴 Exposed credentials
[... continues with 20+ more issues across entire codebase ...]
```
**Result:**
- ✅ Comprehensive analysis in single prompt
- ✅ Consistent recommendations
- ✅ Prioritized action plan
- ✅ Completed in 5 minutes vs 2-3 hours
---
#### Use Case 2: Multi-File Refactoring Planning
**Scenario:** Refactor authentication system across 10+ files
**Extended Context Approach:**
**Prompt:**
```
Plan refactoring of authentication system across these files:
FILE 1: src/lib/auth.ts (current implementation)
[paste 500 lines]
FILE 2: src/api/auth/login.ts
[paste 200 lines]
FILE 3: src/api/auth/register.ts
[paste 200 lines]
FILE 4: src/middleware/auth.ts
[paste 150 lines]
FILE 5: src/hooks/useAuth.ts
[paste 300 lines]
FILE 6: src/contexts/AuthContext.tsx
[paste 400 lines]
FILE 7: tests/auth.test.ts
[paste 600 lines]
... [8 more files, total ~5K lines = ~40K tokens]
GOAL: Refactor to:
- Separate business logic from UI logic
- Centralize token management
- Improve testability
- Add proper TypeScript types
- Support multiple auth providers (email, Google, GitHub)
Provide:
1. Refactoring roadmap (step-by-step, maintaining working state)
2. For each step:
- Files to modify
- Before/after code for each change
- Tests to update
- Migration strategy
3. Risk assessment per step
4. Rollback plan if something breaks
```
**Claude Output:**
```markdown
# Auth Refactoring Roadmap
## Step 1: Extract Core Auth Logic (1-2 hours, Low Risk ✅)
### Files to Modify:
1. `src/lib/auth/core.ts` (NEW) - Core auth logic
2. `src/lib/auth/types.ts` (NEW) - TypeScript types
3. `src/lib/auth.ts` (REFACTOR) - Use new core
### Implementation:
#### NEW: src/lib/auth/core.ts
```typescript
// Extract pure business logic
export class AuthService {
private tokenManager: TokenManager;
private apiClient: ApiClient;
async login(credentials: LoginCredentials): Promise<AuthResult> {
// Pure logic, no UI dependencies
const { email, password } = credentials;
const tokens = await this.apiClient.post('/auth/login', { email, password });
await this.tokenManager.setTokens(tokens);
return { success: true, user: tokens.user };
}
async logout(): Promise<void> {
await this.tokenManager.clearTokens();
await this.apiClient.post('/auth/logout');
}
// ... more methods
}
```
#### REFACTOR: src/lib/auth.ts
```typescript
// BEFORE: 500 lines of mixed logic
// AFTER: Thin wrapper around core service
import { AuthService } from './auth/core';
const authService = new AuthService();
export const login = (credentials) => authService.login(credentials);
export const logout = () => authService.logout();
// ...
```
### Tests to Update:
- `tests/auth.test.ts` - Add tests for AuthService class
- Mock TokenManager and ApiClient
### Risk: Low ✅
- Existing API unchanged (backwards compatible)
- Can be done incrementally
- Easy to rollback (delete new files, revert refactor)
---
## Step 2: Centralize Token Management (2-3 hours, Medium Risk ⚠️)
[... detailed step-by-step for remaining 5 steps ...]
```
**Result:**
- ✅ Complete refactoring plan across 10+ files
- ✅ Step-by-step with code snippets
- ✅ Risk assessment per step
- ✅ Testable, incremental migration
---
### 🧠 Context Management Strategies
#### Strategy 1: Hierarchical Context Organization
**Problem:** 200K tokens ≠ infinite memory. Priority matters.
**Solution: Information Hierarchy**
```
HIGH PRIORITY (Always Include):
├─ Current task description
├─ Immediate code context (files being modified)
├─ Error messages / logs
└─ Explicit requirements
MEDIUM PRIORITY (Include if space):
├─ Related code (dependencies, callers)
├─ Architecture overview
├─ Test cases
└─ Documentation
LOW PRIORITY (Omit if needed):
├─ Historical context
├─ Alternative approaches discussed
├─ Tangential code
└─ Verbose logs
```
**Example Optimized Prompt:**
```
TASK: Fix authentication bug in login flow
ERROR LOG (Critical):
[paste exact error, stack trace - 500 tokens]
AFFECTED CODE (Critical):
File: src/api/auth/login.ts
[paste login handler - 1K tokens]
File: src/lib/auth.ts
[paste auth helper - 1K tokens]
DEPENDENCIES (Medium Priority):
File: src/middleware/auth.ts
[paste middleware - 800 tokens]
ARCHITECTURE CONTEXT (Medium):
- JWT tokens stored in httpOnly cookies
- Refresh token rotation enabled
- Token expiry: 15min access, 7d refresh
TESTS (Low Priority, omit for now):
[skip to save tokens, add later if needed]
Total Prompt: ~4K tokens (98% headroom remaining)
```
**Result:** Focused, efficient context usage
---
#### Strategy 2: Context Compression Techniques
**Technique 1: Summarization**
**Instead of:**
```
[paste entire 10K line codebase]
Find all instances of XYZ pattern
```
**Do this:**
```
Summarize this codebase structure:
[paste 10K lines]
Focus on:
- File organization
- Key modules and their responsibilities
- External dependencies
- Architecture patterns used
```
**Then use summary:**
```
Based on this architecture:
[paste Claude's 500-token summary]
Now help me [specific task]
[paste only relevant files - 2K tokens]
```
**Savings:** 10K tokens → 2.5K tokens (75% reduction) ✅
---
**Technique 2: Diff-Based Context**
**Instead of:**
```
Review these 10 files (10K LoC):
[paste entire files]
```
**Do this:**
```
Review this refactoring:
FILES CHANGED (summary):
1. src/auth.ts - Extracted core logic
2. src/api/login.ts - Use new auth service
3. ... (8 more files)
GIT DIFF (only changes):
[paste git diff output - 2K tokens, shows only changes]
CONTEXT:
- Goal: Separate business logic from UI
- Pattern: Service layer architecture
```
**Savings:** 10K tokens → 2K tokens (80% reduction) ✅
---
**Technique 3: Abstract-Then-Detail Pattern**
**Instead of:**
```
[paste everything at once - 50K tokens]
Analyze for issues
```
**Do this - Two-Phase Approach:**
**Phase 1: High-Level Analysis**
```
Analyze this project structure:
FILE TREE (500 tokens):
[paste project tree]
PACKAGE.JSON (300 tokens):
[paste dependencies]
README (200 tokens):
[paste project description]
Identify potential architectural issues at high level.
```
**Claude identifies:** "Potential circular dependencies in auth module, missing tests in payment module"
**Phase 2: Deep Dive (Targeted)**
```
You identified circular dependencies in auth module. Here's the detailed code:
[paste ONLY auth module files - 5K tokens]
Analyze the circular dependency:
- Which files are involved?
- What's the cycle?
- How to break it?
```
**Savings:** 50K tokens → 6K tokens (88% reduction) ✅
---
## 2. Claude-Specific Advanced Features
### 🎨 Artifacts (Interactive Code & Documents)
**What Are Artifacts?**
Artifacts are **interactive, editable outputs** that Claude generates in a separate panel. Unlike regular chat text, artifacts can be:
- Edited and re-run
- Exported independently
- Reused across conversations
**Artifact Types:**
1. **Code** (HTML, JavaScript, React, SVG)
2. **Documents** (Markdown, text)
3. **Diagrams** (Mermaid)
---
#### Artifact Use Case 1: Interactive Prototypes
**Prompt:**
```
Create an interactive Next.js login form prototype:
Requirements:
- Email + password fields
- Client-side validation
- Loading state
- Error handling
- Responsive design (Tailwind)
Make it a working prototype I can test in browser.
```
**Claude Response:**
```typescript
// Artifact: LoginForm.tsx (interactive React component)
import { useState } from 'react';
export default function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
setError('');
// Validation
if (!email || !password) {
setError('Please fill in all fields');
return;
}
if (!/\S+@\S+\.\S+/.test(email)) {
setError('Invalid email format');
return;
}
setLoading(true);
// Simulate API call
await new Promise(resolve => setTimeout(resolve, 1500));
setLoading(false);
alert(`Login successful: ${email}`);
};
return (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<div className="bg-white p-8 rounded-lg shadow-md w-96">
<h2 className="text-2xl font-bold mb-6">Login</h2>
<form onSubmit={handleSubmit}>
<div className="mb-4">
<label className="block text-gray-700 mb-2">Email</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="[email protected]"
/>
</div>
<div className="mb-6">
<label className="block text-gray-700 mb-2">Password</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="••••••••"
/>
</div>
{error && (
<div className="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded">
{error}
</div>
)}
<button
type="submit"
disabled={loading}
className="w-full bg-blue-500 text-white py-2 rounded-lg hover:bg-blue-600 disabled:bg-gray-400 disabled:cursor-not-allowed"
>
{loading ? 'Logging in...' : 'Login'}
</button>
</form>
</div>
</div>
);
}
```
**Benefits:**
- ✅ **Interactive Preview:** See form working in real-time in Claude UI
- ✅ **Editable:** Modify code directly in artifact panel
- ✅ **Exportable:** Copy to your project
- ✅ **Iterative:** Request changes, Claude updates artifact
**Iteration Example:**
```
User: "Add a 'Remember Me' checkbox"
Claude: [updates artifact with checkbox added]
User: "Add Google login button"
Claude: [updates artifact again]
```
**Result:** Rapid prototyping loop (seconds per iteration vs minutes copy-pasting)
---
#### Artifact Use Case 2: Architecture Diagrams
**Prompt:**
```
Create architecture diagram for microservices system:
Services:
- API Gateway (Node.js)
- Auth Service (Python)
- Product Service (Node.js)
- Order Service (Java)
- Payment Service (Node.js)
- Notification Service (Python)
Infrastructure:
- PostgreSQL (Auth, Product, Order)
- Redis (caching, sessions)
- RabbitMQ (async messaging)
- Elasticsearch (search)
Show:
- Service communication (REST, RabbitMQ)
- Data flows
- External integrations (Stripe, SendGrid)
```
**Claude Response (Mermaid Diagram Artifact):**
```mermaid
graph TD
Client[Client Apps]
Gateway[API Gateway<br/>Node.js<br/>:8000]
Auth[Auth Service<br/>Python<br/>:8001]
Product[Product Service<br/>Node.js<br/>:8002]
Order[Order Service<br/>Java<br/>:8003]
Payment[Payment Service<br/>Node.js<br/>:8004]
Notif[Notification Service<br/>Python<br/>:8005]
AuthDB[(PostgreSQL<br/>Auth DB)]
ProductDB[(PostgreSQL<br/>Product DB)]
OrderDB[(PostgreSQL<br/>Order DB)]
Redis[(Redis<br/>Cache/Session)]
Queue[RabbitMQ<br/>Message Queue]
Search[(Elasticsearch<br/>Product Search)]
Stripe[Stripe API]
SendGrid[SendGrid API]
Client -->|REST| Gateway
Gateway -->|REST| Auth
Gateway -->|REST| Product
Gateway -->|REST| Order
Gateway -->|REST| Payment
Auth --> AuthDB
Product --> ProductDB
Order --> OrderDB
Auth --> Redis
Gateway --> Redis
Product --> Search
Order -->|order.created| Queue
Payment -->|payment.processed| Queue
Queue -->|consume| Notif
Payment -->|process payment| Stripe
Notif -->|send email| SendGrid
style Gateway fill:#4CAF50
style Auth fill:#2196F3
style Product fill:#2196F3
style Order fill:#FF9800
style Payment fill:#2196F3
style Notif fill:#9C27B0
```
**Benefits:**
- ✅ **Visual:** Immediate visual representation
- ✅ **Editable:** Modify diagram by editing Mermaid code
- ✅ **Exportable:** PNG, SVG, or Mermaid code
- ✅ **Collaborative:** Share with team for architecture discussions
**Iteration:**
```
User: "Add load balancer before API Gateway"
Claude: [updates diagram with LB]
User: "Show database replication"
Claude: [adds replica databases]
```
---
### 🔧 Analysis Tool (Data Processing & Code Execution)
**What Is It?**
Claude's built-in **Python execution environment** for:
- Data analysis
- File processing
- Calculations
- Code testing
**NOT the same as:** ChatGPT's Code Interpreter (different implementation, different capabilities)
---
#### Analysis Tool Use Case: Large Dataset Processing
**Scenario:** Analyze CSV with API request logs (100K+ rows)
**Prompt:**
```
Analyze this API logs CSV:
[upload api-logs.csv - 5MB file, 100K rows]
Questions:
1. What's the average response time per endpoint?
2. Which endpoints have highest error rates?
3. Identify performance anomalies (>2s response time)
4. Traffic patterns by hour of day
5. Top 10 slowest requests with details
Provide:
- Summary statistics
- Visualizations (charts)
- Specific slow requests to investigate
- Recommendations
```
**Claude Response (Using Analysis Tool):**
```python
# Claude runs this Python code internally:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load data
df = pd.read_csv('api-logs.csv')
# 1. Average response time per endpoint
avg_response = df.groupby('endpoint')['response_time_ms'].mean().sort_values(ascending=False)
# 2. Error rates
error_rate = df.groupby('endpoint').apply(
lambda x: (x['status'].str.startswith('5').sum() / len(x)) * 100
).sort_values(ascending=False)
# 3. Performance anomalies
slow_requests = df[df['response_time_ms'] > 2000]
# 4. Traffic by hour
df['hour'] = pd.to_datetime(df['timestamp']).dt.hour
traffic_by_hour = df.groupby('hour').size()
# 5. Top 10 slowest
top_slowest = df.nlargest(10, 'response_time_ms')[[
'endpoint', 'method', 'response_time_ms', 'status', 'timestamp'
]]
# Visualizations
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
# Plot 1: Avg response time
avg_response.head(10).plot(kind='barh', ax=axes[0,0])
axes[0,0].set_title('Top 10 Slowest Endpoints (Avg)')
axes[0,0].set_xlabel('Avg Response Time (ms)')
# Plot 2: Error rates
error_rate.head(10).plot(kind='barh', ax=axes[0,1], color='red')
axes[0,1].set_title('Top 10 Error Rates (%)')
# Plot 3: Traffic by hour
traffic_by_hour.plot(kind='line', ax=axes[1,0], marker='o')
axes[1,0].set_title('Traffic Patterns by Hour')
axes[1,0].set_xlabel('Hour of Day')
axes[1,0].set_ylabel('Request Count')
# Plot 4: Response time distribution
df['response_time_ms'].plot(kind='hist', bins=50, ax=axes[1,1])
axes[1,1].set_title('Response Time Distribution')
axes[1,1].set_xlabel('Response Time (ms)')
plt.tight_layout()
plt.show()
# Output results
```
**Claude Returns:**
```markdown
## API Logs Analysis Results
### 1. Average Response Time by Endpoint
| Endpoint | Avg Response Time (ms) |
|----------|----------------------|
| POST /api/reports/generate | 3,247 ms | 🔴
| GET /api/analytics/dashboard | 1,832 ms | ⚠️
| POST /api/exports/csv | 1,654 ms | ⚠️
| GET /api/users/search | 421 ms | ✅
| POST /api/auth/login | 234 ms | ✅
**Critical Issues:**
- 🔴 `/api/reports/generate` is 3.2s on average (way too slow!)
- ⚠️ Analytics dashboard taking 1.8s (should be <500ms for UX)
### 2. Error Rates
| Endpoint | Error Rate |
|----------|-----------|
| POST /api/payments/process | 12.3% | 🔴
| GET /api/external/data-sync | 8.7% | 🔴
| POST /api/webhooks/stripe | 5.4% | ⚠️
| GET /api/products/list | 1.2% | ✅
**Critical Issues:**
- 🔴 Payment processing failing 12% of the time!
- 🔴 External data sync unreliable
### 3. Performance Anomalies (>2s)
**Found 1,247 slow requests (1.2% of total)**
**Common patterns:**
- 847 requests (68%) are report generation (expected, heavy operation)
- 234 requests (19%) are search queries with complex filters
- 166 requests (13%) are timeouts waiting for external APIs
**Unexpected:**
- 23 requests to simple endpoints (e.g., `/api/users/:id`) took >2s
→ Investigate: Database connection pool exhaustion?
### 4. Traffic Patterns
**Peak hours:**
- 9-11 AM: 15,234 req/hr (morning report generation)
- 2-4 PM: 12,456 req/hr (afternoon activity)
- 8-10 PM: 8,123 req/hr (evening usage)
**Low hours:**
- 12-6 AM: <1,000 req/hr
**Insight:** Consider scaling up during 9-11 AM peak
### 5. Top 10 Slowest Requests
| Endpoint | Method | Response Time | Status | Timestamp |
|----------|--------|--------------|--------|-----------|
| /api/reports/generate | POST | 45,234 ms | 200 | 2024-01-15 09:23:45 |
| /api/reports/generate | POST | 38,567 ms | 200 | 2024-01-15 09:45:12 |
| /api/analytics/dashboard | GET | 28,345 ms | 504 | 2024-01-16 14:23:11 |
| /api/exports/csv | POST | 25,123 ms | 200 | 2024-01-15 10:12:34 |
| ... | ... | ... | ... | ... |
**Critical Finding:**
- Report generation taking up to 45 seconds (timeout = 30s, some fail!)
- Analytics dashboard timing out (504 errors)
## 📊 Visualizations
[Claude shows 4 charts as described in Python code]
## 🎯 Recommendations
### High Priority 🔴
1. **Fix Payment Processing (12% error rate)**
- Investigate Stripe API integration
- Add retry logic
- Implement idempotency
- Alert on failures
2. **Optimize Report Generation (45s response times)**
- Move to background job queue (BullMQ, Celery)
- Return immediately, email when done
- Add progress tracking
- Implement caching for common reports
3. **Scale Analytics Dashboard (1.8s avg, 28s max)**
- Pre-aggregate data hourly
- Use materialized views
- Implement Redis caching
- Add pagination (don't load everything at once)
### Medium Priority ⚠️
4. **External API Reliability (8.7% error rate)**
- Add circuit breaker pattern
- Implement exponential backoff
- Cache responses when possible
- Add fallback data
5. **Database Connection Pool**
- Investigate simple endpoint slowness (23 cases >2s)
- Increase pool size if needed
- Add connection monitoring
### Long-Term Improvements ℹ️
6. **Auto-Scaling Based on Traffic**
- Scale up at 8 AM (before 9 AM peak)
- Scale down at 11 PM
- Save ~40% infrastructure cost
7. **Performance Monitoring**
- Set up real-time alerts (>2s response, >5% errors)
- APM tool (DataDog, New Relic)
- Track performance trends
---
**Next Steps:**
1. Create tickets for High Priority items
2. Investigate payment processing logs (detailed error analysis)
3. Design background job system for reports
4. Set up monitoring/alerting
```
**Benefits:**
- ✅ **Fast Analysis:** 100K rows processed in seconds
- ✅ **Visual Insights:** Charts show patterns immediately
- ✅ **Actionable:** Specific recommendations with priorities
- ✅ **No Code Required:** Claude writes Python, you get results
---
### 📝 Projects Feature (Organize Long-Term Work)
**What Are Projects?**
Projects in Claude are **persistent workspaces** with:
- Custom instructions
- Shared knowledge base
- Conversation history
- Context that persists across chats
**Think:** Like a dedicated Claude instance for each project
---
#### Project Setup Example
**Project: "NextJS E-commerce App"**
**Custom Instructions:**
```
You're helping build a Next.js 14 e-commerce application.
TECH STACK:
- Next.js 14 (App Router)
- TypeScript (strict mode)
- PostgreSQL + Prisma
- Tailwind CSS
- tRPC for API
- Stripe for payments
- NextAuth for authentication
CODING STANDARDS:
- Use server components by default
- Client components only when needed ('use client')
- Prefer server actions over API routes
- TypeScript strict mode (no 'any')
- ESLint + Prettier
- 80%+ test coverage target
ARCHITECTURE:
- Repository pattern for data access
- Service layer for business logic
- Separate API types from database types
- Server actions in app/actions/
- Services in lib/services/
- Database in lib/db/
WHEN GIVING CODE:
- Always include TypeScript types
- Include error handling
- Add basic tests
- Consider performance (n+1 queries, etc.)
- Security (input validation, SQL injection, XSS)
PROJECT CONTEXT:
- Current phase: MVP development
- Priority: Core e-commerce features (cart, checkout, orders)
- Timeline: Launching in 6 weeks
```
**Knowledge Base (Upload Files):**
- `architecture.md` - System design doc
- `api-spec.md` - API contracts
- `database-schema.md` - Prisma schema
- `coding-standards.md` - Team conventions
---
**Usage in Conversations:**
**Chat 1 (Product Implementation):**
```
User: "Implement product catalog with search and filters"
Claude: [References custom instructions automatically]
I'll implement this following your architecture:
- Server component for product list (app/products/page.tsx)
- Server action for filters (app/actions/products.ts)
- Service layer (lib/services/productService.ts)
- Repository (lib/db/repositories/productRepository.ts)
[Provides implementation following project standards]
```
**Chat 2 (Days Later, Different Feature):**
```
User: "Add shopping cart functionality"
Claude: [Remembers project context from Project knowledge base]
Based on your existing architecture (from architecture.md):
- I'll use the same repository pattern
- Follow the auth flow you established
- Integrate with your existing product service
[Consistent with previous work, no need to re-explain project structure]
```
**Benefits:**
- ✅ **Consistency:** Claude remembers project conventions
- ✅ **No Repetition:** Don't re-explain tech stack every chat
- ✅ **Faster:** Jump straight to work
- ✅ **Team Sharing:** Share project with team members
---
## 3. Tool Use & Function Calling
### 🛠️ What Is Tool Use?
**Tool Use** = AI model calling external functions/APIs to complete tasks
**How It Works:**
1. You define available tools (functions with descriptions)
2. AI decides when to call which tool
3. AI constructs proper function call with parameters
4. Your code executes the function
5. AI uses result to continue task
**Why It Matters:**
- ✅ AI can access real-time data (stock prices, weather, databases)
- ✅ AI can perform actions (send emails, create tickets, deploy code)
- ✅ AI can integrate with your systems (internal APIs, tools)
---
### 📋 Practical Implementation Example
#### Scenario: AI-Powered Customer Support Bot
**Tools Available:**
1. `get_customer_info(email)` - Fetch customer data from database
2. `get_order_status(order_id)` - Check order status
3. `create_support_ticket(customer_email, issue, priority)` - Create ticket in system
4. `send_email(to, subject, body)` - Send email
**Implementation (Claude API + Python):**
```python
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-...")
# Define available tools
tools = [
{
"name": "get_customer_info",
"description": "Retrieves customer information from database by email address. Returns customer name, account status, subscription tier, and join date.",
"input_schema": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "Customer email address"
}
},
"required": ["email"]
}
},
{
"name": "get_order_status",
"description": "Fetches current status of an order by order ID. Returns order status (processing/shipped/delivered), tracking number if available, and estimated delivery date.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "Unique order identifier (format: ORD-XXXXX)"
}
},
"required": ["order_id"]
}
},
{
"name": "create_support_ticket",
"description": "Creates a support ticket in the system. Use when customer issue requires human agent intervention.",
"input_schema": {
"type": "object",
"properties": {
"customer_email": {"type": "string"},
"issue": {
"type": "string",
"description": "Detailed description of the customer issue"
},
"priority": {
"type": "string",
"enum": ["low", "medium", "high", "urgent"],
"description": "Priority level based on issue severity"
}
},
"required": ["customer_email", "issue", "priority"]
}
},
{
"name": "send_email",
"description": "Sends email to customer. Use for order confirmations, shipping updates, or support responses.",
"input_schema": {
"type": "object",
"properties": {
"to": {"type": "string"},
"subject": {"type": "string"},
"body": {"type": "string"}
},
"required": ["to", "subject", "body"]
}
}
]
# Actual tool implementations
def get_customer_info(email):
# Query database
return {
"name": "John Doe",
"email": email,
"status": "active",
"tier": "premium",
"join_date": "2023-05-12"
}
def get_order_status(order_id):
# Query order system
return {
"order_id": order_id,
"status": "shipped",
"tracking": "1Z999AA10123456784",
"estimated_delivery": "2024-01-20"
}
def create_support_ticket(customer_email, issue, priority):
# Create ticket in support system
ticket_id = "TICKET-12345"
return {"ticket_id": ticket_id, "status": "created"}
def send_email(to, subject, body):
# Send via email service
return {"sent": True, "message_id": "msg-xyz"}
# Tool router
def execute_tool(tool_name, tool_input):
if tool_name == "get_customer_info":
return get_customer_info(**tool_input)
elif tool_name == "get_order_status":
return get_order_status(**tool_input)
elif tool_name == "create_support_ticket":
return create_support_ticket(**tool_input)
elif tool_name == "send_email":
return send_email(**tool_input)
# Customer support conversation
def handle_customer_message(user_message):
messages = [{"role": "user", "content": user_message}]
while True:
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
tools=tools,
messages=messages
)
# Check if Claude wants to use a tool
if response.stop_reason == "tool_use":
# Claude decided to call a tool
tool_use_block = next(
block for block in response.content
if block.type == "tool_use"
)
tool_name = tool_use_block.name
tool_input = tool_use_block.input
print(f"🔧 Claude calling tool: {tool_name}")
print(f" Parameters: {tool_input}")
# Execute the tool
tool_result = execute_tool(tool_name, tool_input)
# Send result back to Claude
messages.append({"role": "assistant", "content": response.content})
messages.append({
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": tool_use_block.id,
"content": str(tool_result)
}]
})
# Continue conversation (Claude might call more tools)
continue
else:
# Claude has final response for user
final_response = next(
block.text for block in response.content
if hasattr(block, "text")
)
return final_response
# Example usage
customer_query = """
Hi, I'm [email protected] and I'm checking on my order ORD-12345.
Can you tell me where it is? Also, I need to upgrade my account.
"""
response = handle_customer_message(customer_query)
print(response)
```
**Execution Flow:**
```
User: "Hi, I'm [email protected] and I'm checking on my order ORD-12345. Can you tell me where it is? Also, I need to upgrade my account."
🔧 Claude calling tool: get_customer_info
Parameters: {'email': '[email protected]'}
[Tool returns: {name: "John Doe", tier: "premium", ...}]
🔧 Claude calling tool: get_order_status
Parameters: {'order_id': 'ORD-12345'}
[Tool returns: {status: "shipped", tracking: "1Z999AA10123456784", ...}]
🔧 Claude calling tool: create_support_ticket
Parameters: {
'customer_email': '[email protected]',
'issue': 'Customer wants to upgrade account from premium tier',
'priority': 'medium'
}
[Tool returns: {ticket_id: "TICKET-12345"}]
Claude's Response:
"Hi John! I've checked on your order ORD-12345. Good news - it has been shipped! Your tracking number is 1Z999AA10123456784, and it's estimated to arrive on January 20th.
Regarding your account upgrade: I see you're currently on our premium tier. I've created a support ticket (TICKET-12345) for our billing team to contact you about upgrade options to our enterprise tier. They'll reach out within 24 hours to discuss pricing and features.
Is there anything else I can help you with today?"
```
**Benefits:**
- ✅ **Autonomous:** Claude decides which tools to call
- ✅ **Multi-Step:** Claude can call multiple tools in sequence
- ✅ **Contextual:** Claude uses tool results intelligently
- ✅ **Natural:** User doesn't see technical tool calls
---
### 🚀 Advanced Tool Use Patterns
#### Pattern 1: Sequential Tool Chains
**Scenario:** Deploy code to production
**Tools:**
1. `run_tests()` - Run test suite
2. `build_project()` - Build production bundle
3. `deploy_to_server(version)` - Deploy to production
4. `send_slack_notification(message)` - Notify team
**User Request:** "Deploy the latest changes to production"
**Claude's Execution:**
```
🔧 Call: run_tests()
Result: {passed: 47, failed: 0, coverage: 87%}
✅ Tests passed!
🔧 Call: build_project()
Result: {success: true, bundle_size: "2.3 MB", build_time: "45s"}
✅ Build successful!
🔧 Call: deploy_to_server(version="v1.2.3")
Result: {deployed: true, url: "https://app.example.com", version: "v1.2.3"}
✅ Deployed!
🔧 Call: send_slack_notification(
message="🚀 Deployed v1.2.3 to production. Tests: 47/47 passed. Bundle: 2.3MB"
)
Final Response: "Deployment successful! I've completed the following steps:
1. ✅ Ran all tests (47/47 passed, 87% coverage)
2. ✅ Built production bundle (2.3 MB)
3. ✅ Deployed version v1.2.3 to https://app.example.com
4. ✅ Notified team on Slack
Your changes are now live in production!"
```
**Result:** Multi-step deployment automated ✅
---
#### Pattern 2: Conditional Tool Use
**Scenario:** Smart error handling
**User:** "Check server health"
**Claude's Logic:**
```
🔧 Call: check_server_status()
Result: {status: "unhealthy", error_rate: 15%, response_time: 3500ms}
⚠️ Server unhealthy!
🔧 Call: get_error_logs(limit=10)
Result: [... 10 recent errors ...]
🔧 Call: analyze_errors(logs)
Result: {
root_cause: "Database connection pool exhausted",
affected_endpoints: ["/api/reports", "/api/analytics"]
}
🔧 Call: create_incident(
severity="high",
title="Database connection pool exhausted",
details="..."
)
🔧 Call: send_alert_to_oncall(incident_id="INC-123")
Final Response: "⚠️ Server health issue detected:
**Problem:** Database connection pool exhausted
**Impact:** 15% error rate, 3.5s avg response time
**Affected Endpoints:**
- /api/reports
- /api/analytics
**Actions Taken:**
✅ Created incident INC-123 (high severity)
✅ Alerted on-call engineer
**Immediate Recommendation:**
Increase database connection pool size from 10 to 25 connections.
```
**Result:** Intelligent triage + automated response ✅
---
## 4. Streaming & Real-Time Processing
### 🌊 What Is Streaming?
**Traditional (Non-Streaming):**
```
User: "Write a 1000-word article"
[... waits 30 seconds ...]
Claude: [entire 1000-word article appears at once]
```
**Streaming:**
```
User: "Write a 1000-word article"
Claude: "The future of AI is..." [words appear as they're generated]
```
**Why Streaming Matters:**
- ✅ **Perceived Performance:** User sees progress immediately
- ✅ **Better UX:** Can read while AI is still writing
- ✅ **Interruptible:** Can stop generation early if not relevant
- ✅ **Real-Time Feedback:** See AI's reasoning in real-time
---
### 📋 Streaming Implementation (Claude API)
**Python Example:**
```python
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-...")
# Non-streaming (traditional)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short story"}]
)
print(response.content[0].text) # Waits for complete response
# Streaming (real-time)
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short story"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True) # Print each word as it arrives
```
**Output:**
```
Once upon a time... [appears]
in a small village... [appears]
there lived a brave... [appears]
young programmer... [appears]
... [continues in real-time] ...
```
---
### 🚀 Advanced Streaming: Real-Time Code Generation
**Scenario:** Generate large codebase in real-time
**Implementation:**
```python
import anthropic
import os
client = anthropic.Anthropic()
def generate_codebase_streaming(project_description):
prompt = f"""Generate a complete Next.js project based on this description:
{project_description}
Provide:
1. File structure
2. All source files with complete code
3. package.json
4. README.md
Format each file as:
--- FILE: path/to/file.ts ---
[code]
---
"""
current_file = None
current_content = []
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=8000,
messages=[{"role": "user", "content": prompt}]
) as stream:
for text in stream.text_stream:
# Parse streaming output for file markers
if "--- FILE:" in text:
# Save previous file
if current_file:
save_file(current_file, "".join(current_content))
print(f"✅ Generated: {current_file}")
# Start new file
current_file = text.split("FILE:")[1].split("---")[0].strip()
current_content = []
elif "---" in text and current_file:
# End of file
save_file(current_file, "".join(current_content))
print(f"✅ Generated: {current_file}")
current_file = None
current_content = []
elif current_file:
# Accumulate file content
current_content.append(text)
print(text, end="", flush=True) # Show progress
def save_file(filepath, content):
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w') as f:
f.write(content)
# Usage
project_desc = """
E-commerce product catalog with:
- Product listing with search/filter
- Product detail pages
- Shopping cart
- TypeScript + Next.js 14 + Tailwind
"""
generate_codebase_streaming(project_desc)
```
**Output (Real-Time):**
```
Generating files...
--- FILE: package.json ---
{
"name": "ecommerce-catalog",
"version": "1.0.0",
... [streaming content] ...
}
✅ Generated: package.json
--- FILE: app/page.tsx ---
import { ProductList } from './components/ProductList';
export default function Home() {
... [streaming content] ...
}
✅ Generated: app/page.tsx
--- FILE: app/components/ProductList.tsx ---
'use client';
import { useState, useEffect } from 'react';
... [streaming content] ...
✅ Generated: app/components/ProductList.tsx
[... continues for all files ...]
✅ Complete project generated!
```
**Benefits:**
- ✅ **Progress Visibility:** See each file being generated
- ✅ **Faster Perceived Performance:** Don't wait for entire codebase
- ✅ **Interruptible:** Can stop if going wrong direction
- ✅ **File-by-File Processing:** Save files as they complete
---
## 5. Advanced Prompt Engineering Techniques
### 🎯 System Instructions (Custom Behavior)
**What:** Define persistent AI behavior across all conversations
**Example: Code Review Bot**
```python
system_instructions = """You are a senior software engineer conducting code reviews.
TONE & STYLE:
- Be constructive, not critical
- Explain WHY issues matter (security, performance, maintainability)
- Provide code examples for fixes
- Use emojis for severity: 🔴 Critical, ⚠️ Warning, ℹ️ Info, ✅ Good
REVIEW CHECKLIST:
1. Security (SQL injection, XSS, secrets exposure, auth issues)
2. Performance (n+1 queries, unnecessary re-renders, bundle size)
3. Code Quality (DRY, SOLID, naming, complexity)
4. Testing (missing tests, edge cases, coverage)
5. Best Practices (framework conventions, modern patterns)
OUTPUT FORMAT:
## 🔴 Critical Issues (Fix Now)
[List with file:line, code snippet, fix]
## ⚠️ Warnings (Should Fix)
[...]
## ℹ️ Suggestions (Nice to Have)
[...]
## ✅ Good Practices
[Highlight what's done well]
Always end with estimated time to fix all issues.
"""
# Use in API call
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
system=system_instructions, # <-- Custom behavior
messages=[{
"role": "user",
"content": "Review this code: [paste code]"
}]
)
```
**Result:** Consistent, structured code reviews every time ✅
---
### 🔗 Chain-of-Thought Prompting
**What:** Ask AI to "think step-by-step" before answering
**Example: Complex Algorithm Design**
**Without CoT:**
```
User: "Design algorithm for finding duplicate transactions in large dataset"
Claude: "Here's the algorithm: [jumps to solution, may miss edge cases]"
```
**With CoT:**
```
User: """Design algorithm for finding duplicate transactions in large dataset.
Think through this step-by-step:
1. What defines a "duplicate" transaction?
2. What edge cases exist?
3. What are performance constraints?
4. What data structures would be most efficient?
5. How to handle false positives?
Then provide the final algorithm."""
Claude:
"Let me think through this systematically:
**Step 1: Defining Duplicates**
A duplicate transaction likely means:
- Same amount
- Same timestamp (within a threshold, say ±1 second)
- Same merchant/description
- Same account
But NOT:
- Recurring subscriptions (same amount daily/monthly)
- Split payments (same amount, different merchants)
**Step 2: Edge Cases**
- Clock skew (transactions at 23:59:59 and 00:00:01)
- Partial refunds (same amount, reversed)
- Currency conversion (1.00 USD ≠ 0.85 EUR but might be same transaction)
- Pending vs settled (same transaction appears twice)
**Step 3: Performance Constraints**
- Dataset size: millions of transactions
- Need O(n log n) or better (O(n²) too slow)
- Memory: can't load all in RAM
- Real-time: process as transactions stream in
**Step 4: Data Structures**
Best approach: Hash map with composite key
- Key: (amount, merchant, account, timestamp_bucket)
- Timestamp bucket: round to nearest 5 seconds
- Collisions indicate potential duplicates
Alternative: Sliding window with sorted list
- Sort by timestamp
- For each transaction, check window of ±30 seconds
- Compare attributes
**Step 5: False Positives**
- Two legitimate transactions at same store
- User might buy same item twice
- Solution: Flag for review rather than auto-delete
**Final Algorithm:**
```python
from collections import defaultdict
from datetime import datetime, timedelta
def find_duplicate_transactions(transactions):
# Hash map: composite key → list of transactions
potential_duplicates = defaultdict(list)
confirmed_duplicates = []
for tx in transactions:
# Create composite key (bucket timestamp to 5-second intervals)
timestamp_bucket = (tx.timestamp // 5) * 5
key = (
round(tx.amount, 2),
tx.merchant,
tx.account,
timestamp_bucket
)
potential_duplicates[key].append(tx)
# Review buckets with multiple transactions
for key, tx_list in potential_duplicates.items():
if len(tx_list) > 1:
# Further validation
for i in range(len(tx_list)):
for j in range(i + 1, len(tx_list)):
if are_duplicates(tx_list[i], tx_list[j]):
confirmed_duplicates.append((tx_list[i], tx_list[j]))
return confirmed_duplicates
def are_duplicates(tx1, tx2):
# Fine-grained duplicate check
time_diff = abs(tx1.timestamp - tx2.timestamp)
return (
abs(tx1.amount - tx2.amount) < 0.01 # Same amount (± 1 cent)
and time_diff < 30 # Within 30 seconds
and tx1.merchant == tx2.merchant
and tx1.account == tx2.account
and tx1.status == tx2.status # Both pending or both settled
)
```
**Complexity:**
- Time: O(n) average (O(n log n) if sorting)
- Space: O(n) for hash map
**Handles Edge Cases:**
✅ Clock skew (30-second window)
✅ Currency precision (round to 2 decimals)
✅ Pending vs settled (status check)
✅ False positives (two-level check: bucket + detailed)
**Performance:**
- Can process 1M transactions in ~2-3 seconds
- Memory efficient (hash map stores keys, not full objects)
- Streaming compatible (process in batches)"
```
**Benefits of CoT:**
- ✅ **Better Reasoning:** AI thinks through problem
- ✅ **Fewer Errors:** Catches edge cases
- ✅ **Transparent:** Can see AI's logic
- ✅ **More Accurate:** Final solution is better
---
## 6. Cost Optimization Strategies
### 💰 Token Usage Optimization
**Problem:** API calls are expensive at scale
**Cost Comparison (Claude 3.5 Sonnet):**
- Input: $3 per million tokens
- Output: $15 per million tokens
**Example Costs:**
- 10K input + 2K output = $0.06 per call
- 100 calls/day = $6/day = $180/month
- 1000 calls/day = $60/day = $1,800/month
**Optimization Strategies:**
#### Strategy 1: Prompt Compression
**Inefficient Prompt (10K tokens):**
```
Review this entire codebase:
FILE 1: app/page.tsx (1000 lines)
[paste entire file]
FILE 2: app/layout.tsx (800 lines)
[paste entire file]
... [8 more files, 8000 lines total]
Find security issues.
```
**Optimized Prompt (2K tokens, 80% savings):**
```
Review for security issues:
Security-sensitive files only:
1. src/api/auth.ts (auth logic - 200 lines)
[paste]
2. src/middleware/auth.ts (token validation - 150 lines)
[paste]
3. src/api/users.ts (user operations - 300 lines)
[paste]
Focus: SQL injection, XSS, exposed secrets, insecure auth
```
**Savings:** $0.06 → $0.012 per call (80% cheaper) ✅
---
#### Strategy 2: Caching Results
**Pattern:** Cache AI responses for common queries
```python
import redis
import hashlib
import json
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def get_cached_or_generate(prompt, system_instructions=None):
# Create cache key from prompt
cache_key = hashlib.sha256(
(prompt + str(system_instructions)).encode()
).hexdigest()
# Check cache
cached = redis_client.get(cache_key)
if cached:
print("✅ Cache hit!")
return json.loads(cached)
# Generate (cache miss)
print("❌ Cache miss, calling Claude...")
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
system=system_instructions,
messages=[{"role": "user", "content": prompt}]
)
result = response.content[0].text
# Cache for 24 hours
redis_client.setex(
cache_key,
86400, # 24 hours
json.dumps(result)
)
return result
# Usage
code_review = get_cached_or_generate(
prompt="Review this code: [...]",
system_instructions=code_review_system
)
```
**Savings Example:**
- Same code reviewed 10x/day
- Without cache: 10 API calls/day = $0.60/day
- With cache: 1 API call/day + 9 cache hits = $0.06/day
- **Savings: 90%** ✅
---
#### Strategy 3: Batch Processing
**Inefficient (Multiple Calls):**
```python
# Review 100 files = 100 API calls
for file in files:
review = client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": f"Review {file}"}]
)
# Cost: 100 × $0.06 = $6.00
```
**Optimized (Batching):**
```python
# Batch 10 files per call = 10 API calls
batch_size = 10
for i in range(0, len(files), batch_size):
batch = files[i:i+batch_size]
combined_prompt = "Review these files:
" + "
---
".join(batch)
review = client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": combined_prompt}]
)
# Cost: 10 × $0.15 = $1.50
```
**Savings:** $6.00 → $1.50 (75% cheaper) ✅
---
## Závěr & Action Plan
### ✅ Immediate Next Steps
**Week 1: Learn Extended Context**
1. Try uploading entire codebase to Claude
2. Practice multi-file analysis
3. Use context compression techniques
**Week 2: Master Artifacts**
1. Generate interactive prototypes
2. Create architecture diagrams
3. Use Analysis Tool for data processing
**Week 3: Implement Tool Use**
1. Build simple tool integration (1-2 tools)
2. Test sequential tool chains
3. Add error handling
**Week 4: Optimize Costs**
1. Implement caching
2. Practice prompt compression
3. Batch similar requests
### 📊 Success Metrics
Track these weekly:
- **Token Usage:** Aim for <50K tokens/day
- **Cost:** Target <$5/day for development work
- **Cache Hit Rate:** >60% for repeated queries
- **Time Savings:** Document hours saved vs manual work
### 🚀 Long-Term Mastery (3 Months)
**Month 1:**
- Master extended context + artifacts
- Build first tool integration
- Optimize 50% of prompts
**Month 2:**
- Advanced tool use (3-5 tools)
- Streaming implementation
- Caching system in production
**Month 3:**
- Custom Claude Code workflows
- Full CI/CD integration
- Team rollout & training
**Estimated ROI:**
- Time saved: 40-60% on development tasks
- Quality improvement: Fewer bugs, better architecture
- Cost: $20-40/month for Claude Pro + API usage
- Value: $2,000-4,000/month (based on 60h saved @ $50/hr)
---
**Good luck mastering advanced AI features! Remember: Advanced features are force multipliers - they amplify good practices but also amplify bad ones. Start simple, measure results, iterate. 🚀**
Kde použít tento prompt?
Najděte vhodné AI nástroje pro použití tohoto promptu a maximalizujte jeho efektivitu.
Podobné prompty
Algoritmus optimalizace
Optimalizace algoritmu z O(n²) na O(n log n) nebo lepší.
Programování
Zobrazit
API design s REST best practices
Návrh RESTful API s dokumentací a error handling.
Programování
Zobrazit
CI/CD pipeline setup
Konfigurace GitHub Actions / GitLab CI pipeline s testing a deployment.
Programování
Zobrazit
Claude Extended Context - Práce s 200K Kontextem
Naučte se efektivně využívat Claudeho rozšířené kontextové okno 200K tokenů pro analýzu velkých kódových bází, dokumentů a komplexních projektů.
Programování
Zobrazit
Objevte další AI prompty
Prozkoumejte naši sbírku Programování promptů a najděte ty, které vám pomohou dosáhnout lepších výsledků.