一直很喜歡閱讀,也常從閱讀好書中與讀書會得到許多的力量與啟發,不管是在人生的低潮抑或是順遂的時候。在閱讀之路上,這幾年也保持一個習慣。當閱讀到喜歡的書籍,且那陣子時間允許,就會提醒自己閱讀完後整理出心得筆記。一方面藉機鍛鍊寫作肌肉與思路,方便之後的複習和查閱。另一方面,也可以分享給有需要的朋友們參考,一舉數得。
“不要高估一天達到的成就,不要低估一年累積的成果”
- 自然科普
- 閱讀與生活
熱愛學習、閱讀、美食以及羽球。 深信閱讀、思考、行動與分享的力量。Passionate about lifelong learning, books, good food, and badminton. I believe that growth comes from reading, reflecting, taking action, and sharing our experiences with others.
“不要高估一天達到的成就,不要低估一年累積的成果”
Recently, I’ve had the chance to listen to several really talented colleagues share their hands-on experiences with AI agents, context engineering, and Claude Code.
Along the way, I also spent some time asking Claude Code questions, digging through documentation and articles, and experimenting with different ways of working.
Little by little, I realized that the way I think about AI costs today is actually quite different from where I started.
The AI world moves incredibly fast. What feels like a best practice today might look completely different a few months from now.
But that’s also what makes it interesting.
Every once in a while, I think it’s worth pausing to write down what I’ve learned, what I’ve tried, and maybe more importantly is how my thinking has changed.
My original question about AI efficiency was actually very simple:
Which model is the cheapest to use?
There are so many models now, all with different input and output token prices.
So my first instinct was pretty straightforward:
Just pick a cheaper model. Problem solved, right?
Well… after digging into this for a while, I realized it’s not quite that simple.
What affects AI cost and efficiency isn’t just which model you use.
A lot of it comes down to:
How much stuff are you asking the model to process, and how are you asking it to work?
So before talking about optimization, let’s first look at where AI costs actually come from.
I roughly think about AI costs in three buckets.
Code, API responses, documents, logs, conversation history, all of these eventually become tokens.
And in real-world workflows, the amount of input can be much larger than we realize.
We might see the AI produce a 500-token answer.
But before writing those 500 tokens, it may have already processed tens of thousands of tokens behind the scenes.
For Claude models, output tokens are generally significantly more expensive than input tokens—roughly around 5x for some models.
So if I ask:
“Please generate an extremely detailed, comprehensive report covering absolutely everything.”
Well, that output has a very direct cost. 😅
There’s another important difference between input and output:
Generated output can’t benefit from prompt caching in the same way reusable input can.
New output has to be generated and paid for each time.
That said, in many coding workflows, the amount of output is still much smaller than the amount of input being processed.
This was something I hadn’t paid much attention to before.
Things like:
CLAUDE.md
Memory
Tool schemas
System instructions
MCP tool definitions
Some of these may enter the context every session, or even repeatedly during a conversation.
I think of them a little bit like transaction fees.
You start a session, and there’s already some token cost before you’ve really started doing anything.
If this layer gets too heavy, those costs add up.
Once I realized this, I immediately went back and put my Memory and CLAUDE.md on a little diet. 😂
Another interesting thing I learned is that not every input token necessarily costs the same every time.
If a large block of input:
appears repeatedly,
stays unchanged,
and is reused within the cache window,
then prompt caching can significantly reduce the cost of processing it again.
Relatively stable content like CLAUDE.md or Memory can potentially benefit from this when structured properly.
Newly generated output, on the other hand, doesn’t have the same advantage.
That’s when I realized cost optimization was getting a little more interesting than I originally thought.
It’s not enough to compare:
Which model has the lowest price per million tokens?
You also have to ask:
Where are my tokens actually going?
After using Claude Code for a while, there were definitely sessions where I wanted to tell Claude to take some ginkgo supplements because its memory seemed… not great. 😂
(Although apparently ginkgo doesn’t really help memory anyway, so never mind.)
At first, I didn’t think too deeply about it.
If something was really important, I’d put it into Memory or turn it into a Skill and move on.
Then one of my colleagues mentioned “Lost in the Middle,” and suddenly a few things clicked.
Lost in the Middle describes a common behavior in large language models:
LLMs often handle information near the beginning and end of a long context better than information buried somewhere in the middle.
So as context gets bigger and bigger, agents can start showing some very familiar—and occasionally eye-roll-inducing—symptoms:
Forgetting the original acceptance criteria
Continuing to follow assumptions that are no longer valid
Fixing A and accidentally breaking B
Re-investigating something that was already confirmed earlier
Acting like something you explained ten minutes ago is completely new information
In the past, my reaction was usually something like:
Claude is getting dumb again. 🙄
Now, after the eye roll, I try to ask a different question:
Is my context simply getting too large and too messy?
That small shift in thinking has actually been pretty useful.
Claude Code has a built-in /context command that lets you see how much context you’re currently using and how it’s being allocated.
You can also keep an eye on the Status Line at the bottom of the terminal for a quick view of context usage.
I’ve developed a very simple personal rule:
Once my context gets to around 70%, I start thinking about cleanup.
This isn’t some magical 70% industry standard—it’s simply the threshold that currently works well for me.
If I’m still working on the same task, I’ll usually use /compact to compress and summarize the conversation history before continuing.
If I want to try a different approach or explore a branch while keeping all the history and context I’ve already built up, I’ll use /fork.
And if I’m moving on to a completely different topic?
I just start a new session.
My mental model is basically:
Same task, keep going → /compact
Same context, explore another path → /fork
Different task → New Session
It’s a tiny habit, but I’ve found it surprisingly useful.
I used to keep one session going forever.
Now I’m much more likely to stop and ask:
Do I actually need all of this context for what I’m doing next?
Instead of waiting until the context is almost full and Claude starts “forgetting things” before I finally clean it up. 😂
These days, I don’t look only at:
How much context do I have left?
Because having room left in the context window doesn’t mean the context itself is good.
If it’s filled with outdated discussions, irrelevant files, old assumptions, or information I no longer need, all of that is still competing with the important information for the model’s attention.
So I’ve started thinking about context differently:
The problem isn’t just the number of tokens. It’s the signal-to-noise ratio.
The goal is to preserve the model’s limited attention for the things that actually matter.
Another thing I hadn’t really thought about before was the cost of tools themselves.
I used to have a pretty natural assumption:
More MCP servers = a more capable agent.
But tool definitions also take up context.
Anthropic shared an interesting example: when an AI agent was connected to multiple services such as GitHub, Slack, and Sentry, 58 tool definitions alone consumed around 55K tokens.
By switching to on-demand tool search, the relevant context dropped from roughly 77K tokens to 8.7K tokens—an 85% reduction.
That number really stuck with me.
The agent hasn’t even started working yet.
You’re just telling it:
“Here are all the tools you can use.”
And you’ve already spent a pretty significant amount of context.
There’s another downside to having too many tools available at once:
The agent is also more likely to choose the wrong one.
So more tools don’t automatically mean a better agent.
At this point, I started realizing that asking:
Which model is cheaper?
only optimizes one small piece of the entire AI workflow.
There are many other things that affect both cost and efficiency:
What goes into the context
What gets loaded every session
How tools are connected and designed
How large amounts of data enter the context
How long conversation history is allowed to grow
Whether tasks are properly separated
Which repetitive tasks should become scripts
Which tasks actually need an agent in the first place
And the interesting part is:
These things don’t just affect cost. They directly affect the quality of the AI’s work.
Sometimes saving tokens and improving AI quality are actually the same thing.
Because what you’re removing is often just noise.
In 2025, LangChain discussed four major strategies for managing context in AI agents:
Write / Select / Compress / Isolate
Sometimes summarized as WSCI.
After learning more about token costs, context behavior, and context rot, these four ideas suddenly felt much more practical to me.
I tend to translate them into slightly different words when thinking about my own Claude Code workflow:
What information is stable, important, and likely to be useful again?
That’s the kind of information worth putting into CLAUDE.md, Memory, or another persistent location.
Instead of putting everything into context upfront, search for and retrieve information when it actually becomes relevant.
As context grows, which parts of the history no longer need to be preserved in full?
Maybe all I really need is the conclusion, the decision, and the next step.
Some exploration tasks involve reading a huge number of files and generating lots of intermediate information, while the main agent only needs the final result.
In those cases, it can be cleaner to move the work into another agent or an isolated context and bring back only a high-signal summary.
After reading about these ideas and experimenting with them in Claude Code, I realized that many techniques that initially looked unrelated actually fit nicely into these four categories.
For example:
I started cleaning up my CLAUDE.md and Memory, moving out information that doesn’t need to be loaded every session.
For large logs, JSON files, or CI output, I stopped dumping everything into the AI. I search and filter first, then bring only the relevant information into context.
I started using subagents for broader exploration and bringing only the conclusions back into the main conversation.
Repetitive Jira / Confluence / curl workflows are gradually becoming scripts instead of something I ask an agent to rediscover every time.
Even my coding workflow has slowly changed from:
Think of something → Ask something → Change something → Repeat
to something much more intentional:
Explore → Plan → Implement → Verify → Handoff
All of these changes are really doing the same thing:
Don’t make every piece of information, every task, and every bit of history compete inside the same AI context.
Looking back, one of the most interesting parts of this learning process is how my questions kept changing.
I started with:
Which model is the cheapest?
Then:
How do I save tokens?
Then:
How do I manage context?
And now I find myself thinking about a much more interesting question:
How do I design a better working environment for AI?
Because AI doesn’t necessarily perform better just because it knows more.
In that sense, it’s actually pretty similar to us.
Imagine sitting at a desk with 30 documents spread out in front of you, 10 things on your to-do list, yesterday’s unfinished work still sitting there, and 58 different tools next to you that you have to choose from.
I’m pretty sure most of us wouldn’t be doing our best work either. 😂
So after going down this rabbit hole, this is the idea I keep coming back to:
What we need to manage isn’t just tokens. It’s AI’s attention.
Good context engineering, at least in the way I currently understand it, is largely about doing exactly that:
Putting the right information in front of the AI at the right time.
So… how do we actually do that?
I’ll save that for Part 2.
I want to spend some time doing the experiments and adjustments I’ve been making lately—how I’m applying Persist / Retrieve / Compress / Isolate in Claude Code, what I’ve changed in CLAUDE.md and Memory, how I’m using /compact, /fork, subagents and scripts, and how my coding workflow has gradually evolved along the way.
A lot of this is still experimental for me.
But after actually getting my hands dirty and changing things around, I’ve already found a few pretty interesting lessons worth sharing.
最近陸續聽了公司幾位很優秀的同事分享 AI Agent、Context Engineering 和 Claude Code 的一些實務經驗,自己中間也花了一些時間問問 Claude Code、查資料,再一邊調整自己的做法,慢慢對 AI 成本這件事有了和當初不一樣的心得和觀點。
AI 的世界變化真的很快,很多現在覺得有效的方法,可能過一陣子又會有新的做法。也因為這樣,偶爾停下來整理一下這個階段自己學到什麼、想法怎麼改變,是一件蠻有趣的事情。
到底要用哪一個 Model,才最省錢?
畢竟現在 Model 這麼多,每個模型的 Input / Output Token 價格又不一樣。
一開始很自然會覺得:就選一個比較便宜的 Model,不就省錢了!
但研究了一小陣子之後,我才發現真正影響 AI 成本與效率的,很多時候不只是用了哪個 Model,而是:
你到底讓這個 Model 處理了多少東西,以及怎麼讓它工作。
先來理解AI 的成本到底是怎麼產生的?
AI 的成本,大致可以拆成三類:
程式碼、API Response、文件檔、logs、對話歷史等等,這些全部都會變成 Token。
而實際工作時,Input 的量往往遠大於我們想像。
我們看到 AI 最後可能只回答 500 個 Token,但在回答之前,它可能已經讀了幾萬個 Token。
以 Claude 系列模型來說,Output Token 的單價通常明顯高於 Input,大約可以差到 5 倍。
所以叫 AI 幫我產生一份超級完整、鉅細靡遺的報告,這種 Output 成本是很直接的。
而且 Output 還有一個特性,不像 Input ,Output 無法靠 Prompt Caching 省下來。
每一次產生的Ouput ,就是付完整的 Token。
不過觀察實際運用上面,多數大部分 Coding 任務的 Output 量,還是比 Input 小不少。
這是我以前比較沒有注意到的地方。
例如,CLAUDE.md、Memory、Tool Schema、System Instructions、MCP Tool Definition等,這些東西有些會在每次 Session,甚至每一輪對話裡進入 Context。蠻像手續費,只要一開新的session 就要token。如果這一層很肥,累積起來要付不少token。在認知到這一點後,我馬上去瘦身一下我的 memory 和 Claude.md 那些的(笑)。
這也是一個很有趣的一件事,並不是所有 Input Token 每次都是原價。
如果某一大段 Input 重複出現、沒有改、又在 Cache 有效時間內,就有機會透過 Prompt Caching 降低成本。
像 CLAUDE.md、Memory 這類相對固定的內容,如果設計得好,其實很適合利用 Cache。
反過來看,AI 每次新產生的 Output,則沒有這個優勢。所「成本優化」開始變得比我原本想的複雜一點,不能只是看哪個 Model 每百萬 Token 最便宜,還需要一起看 Token 到底花在哪裡?
用了 Claude Code 一小陣子,有時候某些session 想叫 Claude Code 吃銀杏,記憶力不太好啊.......(雖然據說銀杏證實對記憶力也沒幫助就是了)。 但是這個疑問也只是飄過腦子一下,重要的就要它記錄到 memory,或寫出一個skill 之類的。一直到聽到同事提到 Lost in the Middle ,才讓我兩者對照連結起來。
Lost in the Middle是指:
大型語言模型(LLM)模型通常比較容易掌握 Context 開頭與結尾的資訊,中間大量資訊反而比較容易被忽略。
所以當 Context 越來越肥,Agent 有時候會開始出現一些很熟悉、令人翻白眼的症頭:
忘記一開始的驗收標準
繼續追著已經過時的假設跑
修 A,結果把 B 弄壞
重新調查前面其實已經確認過的事情
明明前面講過了,後面又像第一次看到
以前碰到這些情況,除了狂翻白眼想碎念 Claude 又變笨了之外,現在會深呼吸後先看看:
是不是我的 Context 已經太多太亂了?
在 Claude Code 裡,可以直接用內建的 /context 查看目前 Context 的使用量與分配情況,也可以從終端機底部的 Status Line 快速掌握目前用了多少。
我自己現在有一個很簡單的習慣:
Context 超過大約 70%,我就會開始考慮整理。
如果目前這件事情還要繼續做,我通常會先呼叫 /compact,把前面的對話紀錄壓縮、整理成比較精簡的 Context,再繼續往下。
如果我只是想試試看另一種解法、開一個分支任務,但又希望保留前面已經累積的修改歷史與 Context,我就會用 /fork,從目前狀態分出去探索。
至於完全不同的新主題,我現在就乾脆直接開新的 Session。
簡單來說,我自己的判斷大概是:
同一件事繼續做 → /compact
同一個脈絡,想分支試另一條路 → /fork
已經是另一件事 → New Session
這個習慣看起來很小,但實際用一陣子之後,我覺得蠻有幫助。
以前比較容易一直在同一個 Session 裡聊到底;現在會開始有意識地想:
這些 Context,我下一步真的還需要嗎?
而不是等到整個 Context 快塞滿、Claude 已經開始有點「忘東忘西」的時候,才想起來要整理。😂
現在我比較不會只看:還剩多少 % Context? 因為就算 Window 還沒有塞滿,也不代表目前 Context 的品質很好。
如果裡面充滿過時和不需要的資訊,它們一樣會跟真正重要的資訊競爭 AI 的注意力。
這讓我開始用另外一種方式理解 Context:
Context 的問題,不只是 Token 數量,而是訊號與雜訊的比例。
我們得把有限的注意力留給真正重要的東西。
另一個以前我比較沒注意到的地方,是 Tool。我之前很容易有一個直覺:
MCP Server 越多,Agent 能力越強。
但 Tool Definition 本身也是 Context。
Anthropic 分享過一個案例:當 AI Agent 連接多個服務(如 GitHub、Slack、Sentry 等)累積到 58 個工具時,傳統的一次性載入會消耗約 55K Tokens;改成 On-demand Tool Search 之後,整體相關 Context 從約 77K 降到 8.7K,減少約 85%。
這個數字讓我印象蠻深。還沒有開始「工作」,光是在告訴 AI 你有哪些工具可以用就已經花掉不少錢。
而且工具太多還有另外一個問題:
Agent 選錯工具的機率也會增加。
所以更多 Tool,不一定等於更強的 Agent。
哪個 Model 比較便宜?這個議題其實只是在最佳化整個 AI Workflow 裡的一小部分。
真正影響成本與效率的,還包括很多:
Context 放了什麼
每次固定load什麼
Tool 怎麼連結與設計
大量資料怎麼進來
對話歷史累積多久
任務有沒有被拆開
哪些事情應該交給 Script
哪些事情才真的需要 Agent
這些不只影響 Cost,也直接影響 AI 的工作品質。
省 Token 跟提高 AI 品質,有時候其實是同一件事情。拿掉的,往往就是那些不必要的雜訊。
LangChain 在 2025 年談 Context Engineering for Agents 時,把 Agent 管理 Context 的策略整理成四個方向(簡稱為 WSCI):
Write/Persist、Select/Retrieve、Compress、Isolate
在理解前面提到的 Token 成本、Context Rot 這些概念之後,再回頭看這四個方向,我更能了解它們為什麼實用。
哪些資訊是穩定、重要,而且未來還會一直用到的?
這些才值得放進 CLAUDE.md、Memory,或其他可以長期保存的地方。
不是什麼都先塞進 Context,而是等真正需要時,再去搜尋、讀取、取得相關資訊。
Context 累積到一定程度後,哪些歷史資訊已經不需要保留完整過程,只需要留下結論、決策與下一步?
有些探索會讀大量檔案、產生大量中間資訊,但主 Agent 最後其實只需要結果。
這時候,把工作交給另一個 Agent 或獨立 Context,最後只帶回高訊號的摘要,反而更乾淨。
看了一圈資料,又拿自己的 Claude Code 實驗了一陣子之後,我發現很多原本看起來各自獨立的技巧,其實都可以放回這四個方向理解。
例如:
- 我開始重新整理自己的 CLAUDE.md 和 Memory,把不需要每個 Session 都載入的內容搬出去。
- 遇到很大的 Log、JSON 或 CI Output,不再習慣整包丟給 AI,而是先搜尋、過濾,只讓真正需要的資訊進入 Context。
- 探索範圍很大的工作,開始嘗試交給 Subagent,最後只把結論帶回主要對話。
- 重複出現的 Jira / Confluence / curl 工作,我也開始Script 化
甚至連 Coding Workflow 也慢慢從最初開始的 想到哪、問到哪、改到哪
變成:
Explore → Plan → Implement → Verify → Handoff
這些背後其實都在做同一件事情:
不要讓所有資訊、所有工作、所有歷史,同時擠在 AI 的 Context 裡。
回頭看這段學習過程,我覺得最好玩的是,我問的問題其實一路在變。
從哪個 Model 最省錢?到 怎麼省 Token?後來是怎麼管理 Context?
到現在,我開始去思考 怎麼替 AI 設計一個好的工作環境?
因為 AI 並不是知道越多,就一定做得越好。
這點和跟人很像啊,如果今天桌上同時攤著 30 份文件、10 個待辦事項、昨天做到一半的工作,旁邊還擺著 58 個工具叫你自己挑……應該也很難專心。😂
真正需要管理的,不只是 Token,而是 AI 的注意力。
好的 Context Engineering,某種程度上就是在做這件事情:
讓對的資訊,在對的時間,出現在 AI 面前。
至於到底怎麼做?
下一篇再找時間整理、分享這陣子自己的實驗結果和實際調整,包括怎麼把 Persist / Retrieve / Compress / Isolate 用進 Claude Code,以及我在 CLAUDE.md、Memory、/compact、/fork、Subagent、Script 上做了哪些嘗試,還有這段時間慢慢調整出來的 Coding Workflow。
很多都還在持續實驗中,但也因為真的動手改過一輪,開始有一些蠻有趣的心得可以分享。
喜歡此篇文章的朋友,歡迎轉貼與留言。轉貼時請保持原內容與註明原文標題、連結以及作者即可,謝謝您。
一直很喜歡閱讀,也常從閱讀好書中與讀書會得到許多的力量與啟發,不管是在人生的低潮抑或是順遂的時候。在閱讀之路上,這幾年也保持一個習慣。當閱讀到喜歡的書籍,且那陣子時間允許,就會提醒自己閱讀完後整理出心得筆記。一方面藉機鍛鍊寫作肌肉與思路,方便之後的複習和查閱。另一方面,也可以...