Lesson 80 — Intermediate

Context Caching & Efficiency

System prompt, tool schemas, few-shot examples — yeh har call same jaate hain. Providers ka prefix caching mechanism isi ko exploit karta hai; tumhara kaam structure aisa banao ki cache hit ho, aur phir number se prove karo.

⏱ 10 min• Intermediate• Tool Context Management

Cache mindset

WHAT

Stable context ko prefix mein aage rakhna (cache-friendly order) + har call ke tokens/cost ko measure karna. Optimization se pehle metrics.

WHEN

Har call par same instructions/tools/examples ja rahe hain — chat apps, agent loops, RAG pipelines mein sabse bada reusable chunk yahi hai.

WHERE

Assembly layer ka ordering (stable first) + usage logging (prompt tokens, cached tokens, cost/day, latency).

Mechanism simple hai: provider ek bada common prefix pehchan kar us hisse ko repeated prompt tokens par discount deta hai. Order hi strategy hai — beech mein ek dynamic line daali, prefix toot gaya.

Rule: Stable upar, changing neeche. Timeline/user-specific content hamesha tail mein — wohi cache ka darwaza hai.

Stable vs changing

Prompt ko do hisson mein todo — jo kabhi na badle aur jo har turn badle:

system = RULES + TOOLS + FEW_SHOTS     # stable — har call same prefix
messages = [
    {"role": "system", "content": system},
    {"role": "user", "content": dynamic_input},  # changing sirf tail mein
]
# resp.usage se prompt/cached tokens log karo — hit rate wahi se aayegi
Ek line ka nuksaan: "Aaj ki date: ..." system prompt ke beech mein daal di — prefix badal gaya, discount gaya. Tail mein rakho, rules mein nahi.

Measure — blind nahi

Experiment: Same eval/workload par before/after run karo, phir rollout karo. "Lagta hai kam ho raha hai" optimization nahi hai.

Redundant tokens kaatna

Chhota cleanup, roz ka discount: Har redundant token roz hazaron baar kharch hota hai. Ek baar ka trim, perpetual saving — yehi efficiency hai.

Exercise

Question: Prefix caching ke liye prefix kaisa hona chahiye — stable ya badalta? (ek shabd, English)

Question: Optimization se pehle kya karo — guess ya measure? (ek shabd, English)

Question: Cache-friendly order: stable content pehle, changing content ___ mein? (ek shabd, English)

Common mistakes

Desi example: Hotel ka menu board

Hotel ka menu board fix rehta hai, sirf daily special chalk se badhta hai — board roz nahi banate. Stable prefix wahi board hai: roz same, cache mein baith jaata hai.

text
menu_board = rules + tools + examples   # roz same — cached
special    = aaj_ka_user_input          # chalk — tail mein
Interview angle — yahan se sawaal aate hain
  • Q: Prefix caching mechanism kaise kaam karti hai? (common prefix pehchan kar repeated prompt tokens discount — order se exploit)
  • Q: Kya cache todta hai? (beech ki dynamic line, reordering, schema change)
  • Q: Kya measure karoge? (cached vs fresh tokens, hit rate, cost/day, p50 latency)
  • Q: Redundant tokens kaise kaatoge? (echo removal, single-source schema, trim examples, retrieval dedupe)
Lesson complete?

Caching + measurement toolset mein. Agli lesson: in sabko jod kar poore agent ko wrap karne wali layer — harness engineering.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course