Vibe Coding Best Practices

2026, Jul 22    

Everything has two sides, since the previous blog I talked about some anti-patterns, I will highlight those best practices I learned in the past project and should be adopted to future projects.

Best Practices: Increase Cache Hit Rate (Claude)

Prompt Caching is the most straight forward way to lower the token consumption. When there is a cache hit, usually the price cost only 10% as compared to those standard non-cache token processing. Cache follows the prefix_matching mechanism:

If the initial part of a prompt is the same across multiple requests, the K/V matrix generated can be reused across multiple round. Refer to my previous blog: Decoder Models, calculation of Q/K/V

Based on claude’s official document, any modification on the prefix part of the prompt will lead to the invalidation of the kv cache.

So here are some suggestions developers can follow to max out the cache hit:

  • never intercept and modify the prefix part of the prompts. (such as insertion of timestamps or dynamic content)
  • be cautious to add mcp tools or context information which varies across prompts.
  • adopt those recommended settings:
    • CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 : disable remote control, auto update, error info report. (it helps reducing information leaked to Claude)
    • CLAUDE_CODE_ATTRIBUTION_HEADER=0 : yes, it will add attribution info in header section of the prompt, increase cache miss.

Besides of this, it is also worth of trying to tune the Cache TTL from 5 mins to 1 hour. But yes, which might result in higher caching price, referring to prompt-caching.

Best Practices: The Memory Architectures

There some problems with those traditional agent memory approaches, for example those in OpenClaw:

  • The entire Memory.md file is injected into the system prompt every turn.
  • As the file grows, the token consumptions accumulates.

Nowadays the new generation agents are adopting more advanced memory systems. For example, claude-mem uses a three-stage model:

  1. Hook the main agent output per round, send to a sidecar observer agent
  2. The observer agent refine , format and compress and further save it to SQLite
  3. During session create or execution, the main agent will based on the needs to search SQLite DB for past histories.

Yes, the sidecar agent can consume a lot of token, so that’s why the default sidecar is using claude-haiku (which is relatively cheap and provide a nice ROI). But I believe the skill can be further optimized (maybe have another blog to think about it).

Best Practices: Limit the Context

Use Skill, Not MCP

To reduce the context injected into each session, and especially improving the effectiveness of data protocols. Yes MCPs consumes a lot of token and slows down the prompts. So:

Please use skills instead of MCP. There is no need to create a agent which knows everything but just create agents to do different and specific tasks. Refer to my previous article which I talked about how to build a Generic UI Generator based on pi mono.

Let Agent Run Scripts, Not Write Them

If there are fixed logic or flows everytime a agent need to run. Don’t hesitate to leave it as a fixed script. This helps to save tons of token in complicated flows.

Local Models

Yes, you don’t need to always rely on the agent services provided by OpenAI, Anthropic. You can just leave some simple tasks to be executed locally. Let me give you some cases in my previous works:

  • If the agent need to identify duplicated images or files, you can use PhotoSweeper/dedupeGuru instead of sending the images to claude (you can save 1 dollar per request when using claude 4.8)
  • If you want to conduct voice to text conversion, simply run whisper locally. The distilled version causes around 200MB memory footprint and help you to convert audio to text in milliseconds.

Best Practices: Suitable Model For Suitable Task

At last but not least, most of the time, claude-sonnet/gpt-5.4 is enough for non-coding or automation related works ~ 🐶

TOC