We Cut 94% of AI Coding Tokens With a Local Code Index - Rajkumar Sakthivel, Tesco
AI Engineer · 10 min · 170 sentences · from YouTube's caption track
Each timecode opens YouTube at the start of that sentence. Line anchors (#s42) are the cue ids in the WebVTT, and every line carries its start and end seconds. All transcripts has every talk, and the whole corpus as one file.
- 00:01Hey, I'm Raj.
- 00:02I want to tell a story.
- 00:03Me and my friend Foss, we are building project together.
- 00:08We are using AI coding tools everyday.
- 00:11Cloud code, cursor, co-pilot, code X, normal stuff.
- 00:15One month our AI bill was fine.
- 00:18Next month, huge.
- 00:20We did nothing different.
- 00:22Same project, same tools, just more of it.
- 00:25We panicked.
- 00:27We looked what was happening and we found something surprising.
- 00:31Most of the money was not the AI thinking.
- 00:35Most of it was sending too much context.
- 00:42Files the AI don't need.
- 00:45Context is important.
- 00:48Code that was not relevant sent anyway every time.
- 00:52So, me and my friend Foss, we started to building something to fix it.
- 00:57In this talk, is about what we built and what we learned.
- 01:05Every AI coding tools uh do the same thing.
- 01:11It send your code to the model as a context.
- 01:14And the tools thinks more context is better.
- 01:18We measured typical query on our project.
- 01:22It was sending 45,000 tokens of context, but the part of actually mattered is about 5,000 only.
- 01:29Other 40,000 tokens are not useful, but we paid for them every single query.
- 01:38It's It's like ordering a pizza and paying for extra nine pizzas you don't eat every time.
- 01:49We tried three things before we found what works.
- 01:54First, we changed our prompt be short, only show relevant code.
- 01:59Sounds good.
- 02:00But, it does not work.
- 02:01The model already got 45,000 tokens before it read the prompt.
- 02:06Cost already happened.
- 02:08Second, we change the model setting like a max token temperature.
- 02:12Same problem.
- 02:14These changes the output, not the input.
- 02:17Uh money is in the input.
- 02:19Third, uh output compression.
- 02:22This one actually works.
- 02:26We told the model to write short answers.
- 02:28It cut the output 75%.
- 02:31But, output only uh about 10% of the cost.
- 02:36So, 75% of a small number still small number, not enough.
- 02:42We need to fix the input.
- 02:46This is the most important slide.
- 02:4890% of your AI cost is input.
- 02:51Files, search results, context you send in.
- 02:54Only 10% is output.
- 02:57The code the AI writes back.
- 03:00So, if you cut the output by 75%, you can save about eight 8% total.
- 03:07But, if you cut input by 94%, you can save about 61% total.
- 03:13Same math, but different result.
- 03:15Fix the input.
- 03:17That's where your money goes.
- 03:20We built a local search layer.
- 03:22It sits between your code base and the AI.
- 03:25Instead of sending whole files, the AI search and index.
- 03:29It gets back only small piece of code actually it needs.
- 03:34Here how it works.
- 03:35Five steps.
- 03:36Step one, we read the code and break into small pieces, functions, classes, methods.
- 03:44Not a random chunks, proper piece that makes sense.
- 03:47Step two, we run two searches at at the same time.
- 03:52One such find the code by meaning.
- 03:55One such find the code by exact words.
- 03:58Then combine the results.
- 04:00This is where big saving comes from.
- 04:03Steps three, we can shrink their results even more.
- 04:08Keep only the function name and the description.
- 04:10Cut 50-line function down to five lines.
- 04:14Step four, uh we track the connection with the which function called which.
- 04:20So, if you find one piece of code, you can find everything connected to it.
- 04:26Step five, every results get score.
- 04:29If the score is too low, we don't send it.
- 04:33No bad context.
- 04:34Everything runs on your machine.
- 04:36Nothing goes to the cloud.
- 04:38This is the beauty.
- 04:40Why do we run two searches instead of one?
- 04:44Because each one has a weakness.
- 04:47Meaning-based search is good at finding related ideas, but it misses exact names.
- 04:55You will search for um authenticate user function, and it might show you different auth function instead, because they are similar meaning.
- 05:05The word-based search is good at exact names, but it misses related ideas.
- 05:12You search for login flow, and it misses everything that says sign in.
- 05:17By themselves, both searches miss about one in four results.
- 05:23Together, they miss about one in 10.
- 05:26They fix each other weakness spots.
- 05:31Here is the hard part Foss and I spend most of the time on.
- 05:35The search finds results, but they actually relevant?
- 05:39Sometimes search results returns 10 results, and none of them are right.
- 05:45If they use the bad results, it gives confident wrong answer.
- 05:50The worse than no answer.
- 05:52We tried asking AI to judge its own results, too slow.
- 05:57Add 2-3 seconds every time.
- 05:59We tried to fix the score limit, too simple.
- 06:02Short questions score low even when the match is perfect.
- 06:06It worked.
- 06:08Some simple formula, 50% meaning score, 30% keyword score, 20% how the recent code is.
- 06:17And the limit adjust based on the current result.
- 06:20It runs 0.4 milliseconds, no extra AI calls needed.
- 06:26The lesson we learned, simple formula beats the complex model most of the time.
- 06:33We need numbers, not just stories.
- 06:36So, here are ours.
- 06:38We tested an open-source real project, FastAPI, 53 files, 20 real questions a developer would ask.
- 06:48Without using our tool, 83 uh K tokens per questions.
- 06:53With our tools, uh 4.9 K tokens per questions.
- 06:57That is 94% less.
- 07:00With the extra compression on top, 523 tokens per question.
- 07:06And the accuracy still um find the right code 90% of the code.
- 07:13These numbers are real.
- 07:15Test is public.
- 07:16You can run it yourself.
- 07:18The command is on screen.
- 07:22I want to be honest about the limits.
- 07:25The 94% again the worst case, reading full files every time.
- 07:31In a real life, the tools like a cloud code already smarter than that.
- 07:37Real savings are lower than 94%.
- 07:40We use full file base because it is the only one we can measure the same way every time.
- 07:49Big mixed code base are hard.
- 07:51We tested on large projects with 396 files.
- 07:57The recall dropped almost zero.
- 07:59If your files each do one thing, it works well.
- 08:03If your files do many things, it struggles.
- 08:07We use a small fast model for search.
- 08:10It's quick.
- 08:11Re-indexing takes under a second.
- 08:14But the bigger model would find more.
- 08:17We chose speed over perfection.
- 08:21Simple choices work better than complex one.
- 08:24Small database instead of big infrastructure.
- 08:28Two searches instead of one fancy one.
- 08:31Local instead of cloud.
- 08:33Simple one.
- 08:36Here is something Foss pointed out earlier on.
- 08:39We use many tools.
- 08:40Cloud code for hard problems.
- 08:42Cursor for quick edits.
- 08:44Copilot for small completions.
- 08:47Each tool starts fresh every time.
- 08:51They do not share anything.
- 08:54You explain the same code base to three different days.
- 08:58We built one shared index so all your tools connect to it.
- 09:03Same search, same results for all of them.
- 09:07And we added memory.
- 09:10When one tool learns something about your project, that knowledge stays.
- 09:16Next session, different tool, same project, the context is already there.
- 09:21We explain the code base once.
- 09:25Every tool remembered.
- 09:29This is the saving report on real project.
- 09:32247 queries.
- 09:3412.4 million tokens saved.
- 09:37Nearly 186 not spent.
- 09:40Most of the savings, 84% came from search layer.
- 09:45The rest of them, compression.
- 09:47This is not an estimate.
- 09:49The tool tracks every query.
- 09:51It compares what would have been sent against what was sent.
- 09:57Then it multiplies by the model price.
- 10:00Run it on your project for a week.
- 10:03See your own number.
- 10:05Me and Foss built this because we had a huge bill and no good answer.
- 10:10The answer was not a better model.
- 10:13The answer was sending less.
- 10:15We argue about which model is best, Opus or Sonnet.
- 10:18But the models may be 30% of the cost, but other 70% is what you feed it.
- 10:25Fix the input, the model choice matters less than you think.
- 10:29One command to try it, CCE.
- 10:33It's free, open source.
- 10:34You will see the code on screen.
- 10:36Try it, see the number, tell us what you saved.
- 10:39Thanks.
- 10:40Happy coding.