Lesson 09 � Intermediate Skill
COST OPTIMIZATION:
PURA BUDGET BACHAO, BEST RESULTS PAO.
AI cloud bills bahut badhe ho sakte hain � sahi strategies se 50-80% cost bacha sakte hain. Right-sizing, spot instances, caching, batching � sab practical techniques jo real projects mein kaam karti hain.
WHY: AI cloud costs itne kyun badhte hain?
Ek simple GPT-4 API call 1000 tokens ke liye $0.03 lagta hai. Agar daily 10 lakh calls karo � mahine ka bill $90,000! GPU instances $1-10/hour tak jaate hain. Bina strategy ke cloud bill haath se nikal jaata hai. Lekin sahi approach se same kaam 50-80% saste mein ho sakta hai.
Sahi resource chuno � zyada powerful server lene se zyada paisa jaata hai. Monitoring se pata karo kitna use ho raha hai aur uske hisaab se downgrade karo.
Cheap compute power � cloud providers apne unused capacity bahut saste mein dete hain. Training jobs ke liye ideal, lekin interruption ka risk hai.
Repeat results store karo � same input aaye toh API call skip karo. Redis ya in-memory cache se seconds ki jagah milliseconds mein response.
Group processing � chhote chhote requests ko batch mein club karo. API calls kam hoti hain, bulk discounts milte hain.
RIGHT-SIZING: Sahi resource ka chunav
Jab aap 64GB RAM wala server lete ho aur sirf 8GB use karte ho � baaki 56GB waste hai. Right-sizing ka matlab hai actual usage ke hisaab se resource lena.
# Resource monitoring example
import psutil
def check_resource_usage():
"""Current resource usage dekho"""
cpu = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory()
print(f"CPU Usage: {cpu}%")
print(f"Memory: {memory.used / 1e9:.1f}GB / {memory.total / 1e9:.1f}GB")
print(f"Memory Usage: {memory.percent}%")
# Right-sizing recommendation
if memory.percent < 30:
print(">>> Recommendation: Smaller instance use karo!")
elif memory.percent > 80:
print(">>> Warning: Upgrade karo!")
else:
print(">>> Size sahi hai")
check_resource_usage()Monitoring ke basis pe decisions lo: Agar CPU rarely 30% se upar jaata hai � smaller instance lo. Agar memory 80% se zyada use ho rahi hai � upgrade karo. Ye simple logic lakho bacha sakta hai.
SPOT INSTANCES: Sasta compute power
Cloud providers (AWS, Azure, GCP) apne unused servers bahut saste mein dete hain � 60-90% discount milta hai. Catch: kabhi bhi interrupt ho sakte hain. Training jobs aur batch processing ke liye perfect hai.
# Spot vs On-Demand cost comparison
pricing = {
"g4dn.xlarge": {
"on_demand": 0.526, # per hour
"spot": 0.158, # 70% cheaper!
"monthly_on_demand": 0.526 * 24 * 30,
"monthly_spot": 0.158 * 24 * 30
}
}
instance = pricing["g4dn.xlarge"]
savings = instance["monthly_on_demand"] - instance["monthly_spot"]
print(f"On-Demand: ${instance['monthly_on_demand']:.0f}/month")
print(f"Spot: ${instance['monthly_spot']:.0f}/month")
print(f"Savings: ${savings:.0f}/month ({savings/instance['monthly_on_demand']*100:.0f}%)")
# Output:
# On-Demand: $379/month
# Spot: $114/month
# Savings: $265/month (70%)CACHING: Repeat calls skip karo
Agar same question baar baar aa raha hai � har baar API call kyun karo✓ Cache mein store karo, next time seedha result do. 100ms ki jagah 1ms mein response.
from functools import lru_cache
import time
@lru_cache(maxsize=100)
def expensive_prediction(text):
time.sleep(1) # Simulate API call
return f"Prediction for: {text}"
# First call � full time lagega
start = time.time()
expensive_prediction("hello")
print(f"First: {time.time()-start:.2f}s")
# Cache hit � instant response
start = time.time()
expensive_prediction("hello")
print(f"Cache hit: {time.time()-start:.4f}s")
# Output:
# First: 1.00s
# Cache hit: 0.0001sRedis caching bhi powerful hai � distributed systems mein kaam aata hai. Same concept, lekin multiple servers share kar sakte hain cache.
BATCH PROCESSING: Group mein sasta
Ek ek karke API calls karne se better hai � 10 requests ka batch banao aur ek saath bhejo. Bahut se providers bulk discounts dete hain.
# Single vs Batch API costs
cost_comparison = {
"gpt-4": {"cost_per_1k": 0.03, "quality": "high"},
"gpt-3.5": {"cost_per_1k": 0.002, "quality": "medium"},
"llama-2": {"cost_per_1k": 0.001, "quality": "medium"},
}
print(cost_comparison)
# Cost calculation
tokens_per_request = 500
daily_requests = 10000
for model, info in cost_comparison.items():
daily_cost = (tokens_per_request / 1000) * daily_requests * info["cost_per_1k"]
monthly_cost = daily_cost * 30
print(f"{model}: ${monthly_cost:.0f}/month")
# Output:
# gpt-4: $4500/month
# gpt-3.5: $300/month
# llama-2: $150/month (self-hosted)MODEL SELECTION: Sahi model chuno
Har kaam ke liye GPT-4 zaroori nahi hai. Simple classification ke liye GPT-3.5 ya even smaller models kaafi hain � aur 10-20x saste hain.
# Model routing based on complexity
def select_model(query):
"""Query complexity ke basis pe model select karo"""
simple_keywords = ["hello", "thank", "yes", "no", "hi"]
medium_keywords = ["explain", "summarize", "translate"]
complex_keywords = ["analyze", "compare", "reason", "code"]
query_lower = query.lower()
if any(word in query_lower for word in complex_keywords):
return "gpt-4" # Complex tasks
elif any(word in query_lower for word in medium_keywords):
return "gpt-3.5" # Medium tasks
else:
return "llama-2" # Simple tasks � cheapest!
# Test
queries = [
"Hello!",
"Explain quantum computing",
"Analyze this code and find bugs"
]
for q in queries:
model = select_model(q)
print(f"'{q}' -> {model}")
# Output:
# 'Hello!' -> llama-2
# 'Explain quantum computing' -> gpt-3.5
# 'Analyze this code and find bugs' -> gpt-4Try it: API Cost Optimizer
Apne API usage ko optimize karo � caching, batching, aur model selection se kitna bacha sakte ho calculate karo.
Exercise: Test Your Knowledge
Quick check
"Caching se kaise bachte hain?"
Socho: Same question baar baar pucha ja raha hai. Har baar API call karna padega ya koi shortcut hai?
Key Takeaways
- Right-Sizing: Monitoring se actual usage pata karo, uske hisaab se resource lo � zyada powerful lene ki zaroorat nahi.
- Spot Instances: Training jobs ke liye 60-90% discount milta hai � interruption handle karna seekho.
- Caching: Same input pe cached result return karte hain � API call skip, response instant.
- Batching: Chhote requests ko group mein bhejo � bulk discounts aur kam overhead.
- Model Routing: Har query ke liye GPT-4 zaroori nahi � simple tasks ke liye sasta model use karo.
- Monitor & Optimize: Weekly bills analyze karo, patterns dhundho, continuously improve karo.
Ab Capstone Project par chalo � sab seekhi hui cheezein (Docker, Cloud, MLOps, Security, Cost Optimization) ek saath use karke ek complete AI application banao.