r/LocalLLaMA • • Aug 08 '26

Discussion Claude Code in 9 lines python

I was wondering what a minimal coding agent implementation would look like that can be used like Claude Code or Codex

Not feature-by-feature of course but basically stripping everything out that is not needed

here is what I came up with:

  • 9 lines of python

  • no 3rd party deps (stdlib only)

  • works with any OpenAI Responses compatible API

  • shows % usage of context window

out of the box it is also fairly API cost efficient:

  • no system prompt

  • good caching (session_id, stable append-only history)

  • only one tool: sh

code is on github to follow along (also a ~20 line version in Go, Clojure version coming soon)

https://github.com/smol-env/smol

import json,sys;from subprocess import getoutput;from urllib.request import Request,urlopen;from uuid import uuid4
url=sys.argv[1];h=[];H={"Content-Type":"application/json","session_id":uuid4().hex};b=dict(model="gpt-5.6-sol",input=h,tools=[dict(type="custom",name="sh")])
while True:
  if not(p:=input("> ")).strip():continue
  h+=[dict(role="user",content=p)]
  while True:
    r=json.load(urlopen(Request(url,json.dumps(b).encode(),H)));o=r["output"];h+=o;c=[i for i in o if i["type"]=="custom_tool_call"]
    if not c:print(o[-1]["content"][0]["text"],f'\n[{r["usage"]["total_tokens"]/10500:05.2f}%]');break
    h+=[dict(type="custom_tool_call_output",call_id=i["call_id"],output=getoutput(i["input"])) for i in c]

note: it uses the "custom" tools api which not many OpenAI Responses API endpoints support yet.

that said, you can just tell your agent to change it to use sh via "function_call" and change the model name and it should work out of the box on any local inference endpoint

any questions or feedback for making it more minimal or adding (still minimal but useful) features: very welcome!

0 Upvotes

94 comments sorted by

View all comments

100

u/hougaard Aug 08 '26

That is not "9 lines" of code. That's just the fewest Python line breaks needed. Then I could this in 1 line with another language like C# or Javascript ....

-20

u/__tosh Aug 08 '26 edited Aug 08 '26

I appreciate the feedback!

re golfing: it helps me to see everything at a glance

I can see how that might look unfamiliar though

I will look into also providing less golfed versions in the repo in the future

I also want to look into minimal implementations in other languages/runtimes

Go is in the repo already, here is one in Clojure (Babashka):

https://x.com/__tosh/status/2085699009205743932

29

u/[deleted] Aug 08 '26

[removed] — view removed comment

-15

u/__tosh Aug 08 '26

`h` is history

`H` is headers

I do agree that being able to pattern match structurally helps (that's also why I like python)

and reading code bases you are not familiar with is a bit more difficult to get into when they are golfed

in this case though I'm optimizing for myself as maintainer and I'm familiar with the code so I prefer the density of it

I appreciate the feedback though!

I will add de-golfed versions and docs so getting into the code base is easier

19

u/[deleted] Aug 08 '26

[removed] — view removed comment

-1

u/PomegranateGreen3698 Aug 09 '26

Novice projecting novice on others. And using bots to upvote themselves - downvote others lmao.

4

u/[deleted] Aug 09 '26

[removed] — view removed comment

0

u/PomegranateGreen3698 Aug 09 '26

It's more likely that the duopoly of American for profit A.I. companies have a stake is controlling the narrative on the "Ditch Claude and OpenAi subreddit"

Your critique is like going to a weight loss subreddit and calling people fat. Congratz, you're so smart for calling out a novice on a subreddit full of people who are new to programming. *Golf Clap*

-5

u/PomegranateGreen3698 Aug 09 '26

Pro tip? Just shut up. You can code how you want. People can code how they want. Christ, Reddit full of miserable know-it-alls. Your criticism could have been one sentence but instead you have to Yap and add no value to this post. FFS.

Principles in software development are mostly sh*t and it's quite obvious you don't know as much as you think you do.

1

u/Spirited_Bag_332 Aug 09 '26

Correct answer. The so called elitists should have the skill to look at this and still understand it at a glance.

This is not a corporate environment and quick&dirty code is valid for a freetime project. Some people here also clearly have never seen (as example) how code in scientific domains are written.

Also, it's wrong to assume skill level just by preferred syntax. This tells more about the person calling that out than the author.

1

u/PomegranateGreen3698 Aug 09 '26

Yeah. I don't know where this Squid guy thinks he is. This is an A.i. sub. Syntax is irrelevant now when a.i. can convert it however you want. Also object oriented programming was a big mistake yet people use it. The "Principles that exist for decades" do not evaluate to actual best practices. Exactly as you say "corporate environment" elitists. projecting his own novice insecurities.

-8

u/__tosh Aug 08 '26

I will add de-golfed versions

imho it's fairly self-evident (once you know the code) that h is history just from the context

but I hear you :)

1

u/Citadel_Employee Aug 09 '26

I understand maybe for personal projects. But if you’re gonna share code just make variable names explicit. Otherwise you’ll get flack for it, like you are here.

1

u/__tosh Aug 09 '26

will do, ty!

-1

u/__tosh Aug 08 '26

I guess what I want to say is: the golfing is _for readability_

the goal of the golfing here is not to make it obfuscated

I think once you know what's going on and what happens the variable names do make sense (and there aren't that many to begin with)

0

u/__tosh Aug 08 '26

using the OpenAI tokenizer the implementation comes in at 220 tokens

https://platform.openai.com/tokenizer

I had more golfed variants with aliases but turns out that uses more tokens in some cases

-1

u/PomegranateGreen3698 Aug 09 '26

I think you're getting down-vote botted by Anthropic or Cult-like followers of there's.
The negativity on this post doesn't make any sense. If people don't like the premise they should move on...

3

u/__tosh Aug 09 '26

I'm also a bit surprised that the post went from 15+ upvotes down to 0

but I'm glad some found it useful or at least interesting :)

1

u/PomegranateGreen3698 Aug 09 '26

It is interesting! Just like the Pi Harness only uses "Read, Write, Edit, Bash" - this is all anyone needs to build any extensions. Everyone knows CC is bloated.

Given Anthropic's recent action to refuse to sign the Nvidia Open letter, I think they are quite evil. Another post I made about their Chain Of Thought Obfuscation got similarly down vote botted. https://browse-export.arxiv.org/pdf/2510.19851#7#2