%3Cp%3EThree%20months%20of%20production%20experience%20with%20Claude%20%2B%20MCP%20teaches%20you%20things%20that%20no%20documentation%20covers.%20The%20retry%20patterns%20that%20actually%20work.%20The%20system%20prompts%20that%20reduce%20hallucinated%20tool%20calls.%20The%20caching%20strategies%20that%20cut%20your%20bill%20in%20half.%20The%20error%20classes%20you%20will%20encounter%20and%20the%20ones%20that%20silently%20corrupt%20output.%20This%20lesson%20consolidates%20those%20hard-won%20patterns%20into%20a%20reference%20you%20can%20apply%20directly.%3C/p%3E%0A%0A%3Cfigure%20style%3D%22margin%3A2em%200%3Btext-align%3Acenter%3B%22%3E%0A%20%20%3Cimg%20src%3D%22https%3A//sudoall.com/wp-content/uploads/2026/03/mcp-claude-prod-hero.jpeg%22%20alt%3D%22Production%20Claude%20MCP%20patterns%20overview%20diagram%20showing%20caching%20retry%20budget%20observability%20blocks%20dark%22%20style%3D%22max-width%3A100%25%3Bborder-radius%3A8px%3B%22%20/%3E%0A%20%20%3Cfigcaption%20style%3D%22color%3A%23888%3Bfont-size%3A0.9em%3Bmargin-top%3A0.5em%3B%22%3EProduction%20Claude%20%2B%20MCP%3A%20the%20patterns%20that%20separate%20reliable%20systems%20from%20ones%20that%20fail%20at%202am.%3C/figcaption%3E%0A%3C/figure%3E%0A%0A%3Ch2%3EPrompt%20Caching%20for%20Cost%20Reduction%3C/h2%3E%0A%0A%3Cp%3EAnthropic%27s%20prompt%20caching%20feature%20caches%20portions%20of%20the%20input%20prompt%20that%20do%20not%20change%20between%20requests.%20For%20MCP%20applications%2C%20the%20tool%20definitions%20and%20system%20prompt%20are%20perfect%20candidates%20-%20they%20are%20typically%20the%20same%20for%20every%20user%20in%20a%20session.%20Caching%20them%20can%20reduce%20costs%20by%2050-90%25%20on%20repeated%20calls.%3C/p%3E%0A%0A%3Cpre%3E%3Ccode%3E//%20Enable%20prompt%20caching%20for%20stable%20content%0Aconst%20response%20%3D%20await%20anthropic.messages.create%28%7B%0A%20%20model%3A%20%27claude-3-5-sonnet-20241022%27%2C%0A%20%20max_tokens%3A%204096%2C%0A%20%20system%3A%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20type%3A%20%27text%27%2C%0A%20%20%20%20%20%20text%3A%20%60You%20are%20a%20helpful%20assistant%20with%20access%20to%20our%20product%20database%20and%20order%20management%20system.%0AAlways%20verify%20product%20availability%20before%20confirming%20orders.%0AFormat%20all%20prices%20in%20USD.%60%2C%0A%20%20%20%20%20%20cache_control%3A%20%7B%20type%3A%20%27ephemeral%27%20%7D%2C%20%20//%20Cache%20this%20system%20prompt%0A%20%20%20%20%7D%2C%0A%20%20%5D%2C%0A%20%20tools%3A%20claudeTools.map%28t%20%3D%3E%20%28%7B%0A%20%20%20%20…t%2C%0A%20%20%20%20//%20Cache%20tool%20definitions%20-%20they%20rarely%20change%0A%20%20%20%20…%28claudeTools.indexOf%28t%29%20%3D%3D%3D%20claudeTools.length%20-%201%0A%20%20%20%20%20%20%3F%20%7B%20cache_control%3A%20%7B%20type%3A%20%27ephemeral%27%20%7D%20%7D%20%20//%20Cache%20after%20last%20tool%20definition%0A%20%20%20%20%20%20%3A%20%7B%7D%29%2C%0A%20%20%7D%29%29%2C%0A%20%20messages%2C%0A%7D%29%3B%0A%0A//%20Check%20cache%20performance%20in%20usage%20stats%0Aconst%20usage%20%3D%20response.usage%3B%0Aconsole.error%28%60Cache%3A%20%24%7Busage.cache_read_input_tokens%7D%20hit%2C%20%24%7Busage.cache_creation_input_tokens%7D%20created%60%29%3B%0A%3C/code%3E%3C/pre%3E%0A%0A%3Cblockquote%20style%3D%22border-left%3A4px%20solid%20%2300bcd4%3Bmargin%3A1.5em%200%3Bpadding%3A0.5em%201.5em%3Bcolor%3A%23aaa%3Bfont-style%3Aitalic%3B%22%3E%0A%22Prompt%20caching%20enables%20you%20to%20cache%20portions%20of%20your%20prompt.%20Cached%20data%20is%20stored%20server-side%20for%20a%20rolling%205-minute%20period%2C%20after%20which%20it%20expires.%20Cache%20hits%20save%2090%25%20of%20input%20token%20costs%20for%20the%20cached%20portion.%22%20-%20%3Ca%20href%3D%22https%3A//docs.anthropic.com/en/docs/build-with-claude/prompt-caching%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EAnthropic%20Documentation%2C%20Prompt%20Caching%3C/a%3E%0A%3C/blockquote%3E%0A%0A%3Ch2%3ESystem%20Prompt%20Patterns%20That%20Work%3C/h2%3E%0A%0A%3Cp%3EClaude%20responds%20better%20to%20system%20prompts%20that%20describe%20the%20persona%2C%20define%20tool%20usage%20rules%2C%20specify%20output%20format%2C%20and%20set%20boundaries%20-%20in%20that%20order.%20Vague%20system%20prompts%20produce%20vague%20tool%20use.%3C/p%3E%0A%0A%3Cpre%3E%3Ccode%3Econst%20PRODUCTION_SYSTEM_PROMPT%20%3D%20%60You%20are%20a%20precise%20product%20research%20assistant%20for%20TechStore.%0A%0ATOOL%20USAGE%20RULES%3A%0A1.%20Always%20call%20search_products%20before%20making%20any%20recommendations%0A2.%20For%20price%20comparisons%2C%20call%20get_product_price%20for%20each%20product%20separately%0A3.%20If%20a%20product%20has%20less%20than%203%20reviews%2C%20note%20%22limited%20reviews%22%20in%20your%20response%0A4.%20Never%20recommend%20products%20that%20are%20out%20of%20stock%20%28use%20check_availability%20first%29%0A5.%20If%20tools%20return%20errors%2C%20explain%20what%20you%20could%20not%20verify%20rather%20than%20guessing%0A%0AOUTPUT%20FORMAT%3A%0A-%20Lead%20with%20the%20recommendation%2C%20then%20supporting%20evidence%0A-%20Include%20price%2C%20rating%2C%20and%20availability%20for%20each%20recommended%20product%0A-%20Use%20bullet%20points%20for%20product%20comparisons%0A-%20End%20with%20%22Note%3A%20Stock%20and%20prices%20verified%20at%20%5Bcurrent%20timestamp%5D%22%0A%0ABOUNDARIES%3A%0A-%20You%20can%20only%20recommend%20products%20from%20our%20catalogue%0A-%20Do%20not%20speculate%20about%20products%20not%20in%20the%20search%20results%0A-%20If%20the%20user%20asks%20for%20something%20outside%20our%20catalogue%2C%20say%20so%20clearly%60%3B%0A%3C/code%3E%3C/pre%3E%0A%0A%3Cfigure%20style%3D%22margin%3A2em%200%3Btext-align%3Acenter%3B%22%3E%0A%20%20%3Cimg%20src%3D%22https%3A//sudoall.com/wp-content/uploads/2026/03/mcp-claude-prod-caching.jpeg%22%20alt%3D%22Anthropic%20prompt%20caching%20diagram%20showing%20system%20prompt%20and%20tool%20definitions%20cached%20versus%20messages%20uncached%20cost%20reduction%22%20style%3D%22max-width%3A100%25%3Bborder-radius%3A8px%3B%22%20/%3E%0A%20%20%3Cfigcaption%20style%3D%22color%3A%23888%3Bfont-size%3A0.9em%3Bmargin-top%3A0.5em%3B%22%3EPrompt%20caching%3A%20static%20content%20%28system%20prompt%2C%20tool%20definitions%29%20cached%20at%2090%25%20discount%3B%20dynamic%20messages%20are%20not%20cached.%3C/figcaption%3E%0A%3C/figure%3E%0A%0A%3Ch2%3EProduction%20Error%20Taxonomy%3C/h2%3E%0A%0A%3Cpre%3E%3Ccode%3E//%20Claude%20API%20errors%20and%20how%20to%20handle%20them%0A%0A//%20429%20-%20Rate%20limit%3A%20retry%20with%20exponential%20backoff%0A//%20529%20-%20Overloaded%3A%20retry%20with%20longer%20backoff%20%28Anthropic%20load%29%0A//%20400%20-%20Bad%20request%3A%20check%20tool%20schema%2C%20messages%20format%2C%20max_tokens%0A//%20401%20-%20Auth%20error%3A%20check%20ANTHROPIC_API_KEY%0A//%20413%20-%20Request%20too%20large%3A%20trim%20context%20or%20summarize%20conversation%20history%0A%0A//%20Non-error%20patterns%20to%20watch%3A%0A//%20stop_reason%20%3D%3D%3D%20%27max_tokens%27%20-%20response%20was%20cut%20off%2C%20increase%20max_tokens%0A//%20stop_reason%20%3D%3D%3D%20%27end_turn%27%20but%20no%20text%20-%20model%20may%20be%20stuck%2C%20check%20context%0A%0Aasync%20function%20callClaudeWithRetry%28params%2C%20maxRetries%20%3D%203%29%20%7B%0A%20%20for%20%28let%20attempt%20%3D%201%3B%20attempt%20%3C%3D%20maxRetries%3B%20attempt%2B%2B%29%20%7B%0A%20%20%20%20try%20%7B%0A%20%20%20%20%20%20return%20await%20anthropic.messages.create%28params%29%3B%0A%20%20%20%20%7D%20catch%20%28err%29%20%7B%0A%20%20%20%20%20%20const%20shouldRetry%20%3D%20err.status%20%3D%3D%3D%20429%20%7C%7C%20err.status%20%3D%3D%3D%20529%20%7C%7C%20err.status%20%3E%3D%20500%3B%0A%20%20%20%20%20%20if%20%28%21shouldRetry%20%7C%7C%20attempt%20%3D%3D%3D%20maxRetries%29%20throw%20err%3B%0A%0A%20%20%20%20%20%20const%20delay%20%3D%20Math.min%281000%20%2A%20Math.pow%282%2C%20attempt%29%2C%2030000%29%3B%0A%20%20%20%20%20%20const%20retryAfter%20%3D%20err.headers%3F.%5B%27retry-after%27%5D%0A%20%20%20%20%20%20%20%20%3F%20parseInt%28err.headers%5B%27retry-after%27%5D%29%20%2A%201000%0A%20%20%20%20%20%20%20%20%3A%20delay%3B%0A%0A%20%20%20%20%20%20console.error%28%60%5Bclaude%5D%20Attempt%20%24%7Battempt%7D%20failed%20%28%24%7Berr.status%7D%29%2C%20retrying%20in%20%24%7BretryAfter%7Dms%60%29%3B%0A%20%20%20%20%20%20await%20new%20Promise%28r%20%3D%3E%20setTimeout%28r%2C%20retryAfter%29%29%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%3C/code%3E%3C/pre%3E%0A%0A%3Ch2%3EContext%20Management%20for%20Long%20Conversations%3C/h2%3E%0A%0A%3Cpre%3E%3Ccode%3E//%20Summarise%20old%20conversation%20history%20when%20approaching%20context%20limits%0A//%20Claude%203.5%20Sonnet%20context%3A%20200K%20tokens%20%28allows%20very%20long%20conversations%29%0A//%20But%20cost%20grows%20linearly%20with%20context%20-%20summarize%20for%20efficiency%0A%0Aasync%20function%20summariseHistory%28messages%2C%20anthropicClient%29%20%7B%0A%20%20const%20summaryRequest%20%3D%20await%20anthropicClient.messages.create%28%7B%0A%20%20%20%20model%3A%20%27claude-3-5-haiku-20241022%27%2C%20%20//%20Use%20cheaper%20model%20for%20summarisation%0A%20%20%20%20max_tokens%3A%20500%2C%0A%20%20%20%20messages%3A%20%5B%0A%20%20%20%20%20%20…messages%2C%0A%20%20%20%20%20%20%7B%20role%3A%20%27user%27%2C%20content%3A%20%27Summarise%20our%20conversation%20so%20far%20in%203%20bullet%20points%2C%20preserving%20all%20key%20facts%20found%20via%20tool%20calls.%27%20%7D%2C%0A%20%20%20%20%5D%2C%0A%20%20%7D%29%3B%0A%20%20return%20summaryRequest.content%5B0%5D.text%3B%0A%7D%0A%0A//%20In%20your%20main%20conversation%20loop%2C%20check%20token%20usage%3A%0Aif%20%28response.usage.input_tokens%20%3E%2050000%29%20%7B%0A%20%20const%20summary%20%3D%20await%20summariseHistory%28messages%2C%20anthropic%29%3B%0A%20%20messages%20%3D%20%5B%7B%20role%3A%20%27user%27%2C%20content%3A%20%60Previous%20conversation%20summary%3A%5Cn%24%7Bsummary%7D%60%20%7D%5D%3B%0A%7D%0A%3C/code%3E%3C/pre%3E%0A%0A%3Ch2%3EFailure%20Mode%3A%20model%20Outputting%20Tool%20Calls%20That%20Do%20Not%20Exist%3C/h2%3E%0A%0A%3Cpre%3E%3Ccode%3E//%20Claude%20occasionally%20hallucinates%20tool%20names%2C%20especially%20if%20tool%20descriptions%20are%20vague%0A//%20Guard%20against%20this%20at%20the%20execution%20layer%0Aconst%20toolNames%20%3D%20new%20Set%28mcpTools.map%28t%20%3D%3E%20t.name%29%29%3B%0A%0Afor%20%28const%20toolUse%20of%20toolUseBlocks%29%20%7B%0A%20%20if%20%28%21toolNames.has%28toolUse.name%29%29%20%7B%0A%20%20%20%20console.error%28%60%5Bwarn%5D%20Claude%20called%20non-existent%20tool%3A%20%24%7BtoolUse.name%7D%60%29%3B%0A%20%20%20%20toolResults.push%28%7B%0A%20%20%20%20%20%20type%3A%20%27tool_result%27%2C%0A%20%20%20%20%20%20tool_use_id%3A%20toolUse.id%2C%0A%20%20%20%20%20%20content%3A%20%5B%7B%20type%3A%20%27text%27%2C%20text%3A%20%60Tool%20%27%24%7BtoolUse.name%7D%27%20does%20not%20exist.%20Available%20tools%3A%20%24%7B%5B…toolNames%5D.join%28%27%2C%20%27%29%7D%60%20%7D%5D%2C%0A%20%20%20%20%20%20is_error%3A%20true%2C%0A%20%20%20%20%7D%29%3B%0A%20%20%20%20continue%3B%0A%20%20%7D%0A%20%20//%20…%20execute%20valid%20tool%0A%7D%0A%3C/code%3E%3C/pre%3E%0A%0A%3Ch2%3EWhat%20to%20Check%20Right%20Now%3C/h2%3E%0A%3Cul%3E%0A%20%20%3Cli%3E%3Cstrong%3EEnable%20prompt%20caching%3C/strong%3E%20-%20add%20%3Ccode%3Ecache_control%3A%20%7B%20type%3A%20%27ephemeral%27%20%7D%3C/code%3E%20to%20your%20system%20prompt%20and%20the%20last%20tool%20definition.%20Check%20the%20%3Ccode%3Eusage.cache_read_input_tokens%3C/code%3E%20to%20measure%20savings.%3C/li%3E%0A%20%20%3Cli%3E%3Cstrong%3EAdd%20a%20tool%20existence%20check%3C/strong%3E%20-%20validate%20every%20tool%20name%20Claude%20returns%20before%20attempting%20to%20execute%20it%20via%20MCP.%20Hallucinated%20tool%20calls%20happen%20in%20production.%3C/li%3E%0A%20%20%3Cli%3E%3Cstrong%3EMonitor%20stop%20reasons%3C/strong%3E%20-%20log%20every%20%3Ccode%3Estop_reason%3C/code%3E.%20A%20high%20rate%20of%20%3Ccode%3Emax_tokens%3C/code%3E%20stops%20means%20you%20need%20to%20increase%20%3Ccode%3Emax_tokens%3C/code%3E%20or%20summarize%20context%20sooner.%3C/li%3E%0A%20%20%3Cli%3E%3Cstrong%3EMeasure%20prompt%20cache%20hit%20rates%3C/strong%3E%20-%20aim%20for%20%3E70%25%20cache%20hit%20rate%20in%20sustained%20conversations.%20Low%20hit%20rates%20mean%20your%20%22static%22%20content%20is%20actually%20varying%20between%20calls.%3C/li%3E%0A%3C/ul%3E%0A%0A%3Cp%3EnJoy%20%3B-%29%3C/p%3E
Patterns and Anti-Patterns: Claude and MCP in Production
Posted on
