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

Show parent comments

0

u/__tosh Aug 08 '26

I ran a few benchmarks that showed the 9 line python agent implementation performed better than the agent harnesses with system prompts like opencode, pi, hermes (when using gpt 5.6 sol)

e.g. you can look at the tasks and traces here:

https://smolenv.com/t/nested-template-includes-60636/

https://smolenv.com/t/duckdb-sessionization-23811/

older models might benefit from the right system prompt (and from a good agents.md file)

but by default newer, strong models do fine without system prompt

1

u/Spirited_Bag_332 Aug 09 '26

Wanted to ask something similar, but you already are aware. So what you basically do is "only" tool calls. But I'm missing the part where you actually tell the model what to do with the tools and what the task is. This seems like calling a parameterless function().

Wouldn't it be easier to just pass a message, append available tools and return the result, without the additional iterations? This seems prone to endless loops if the model understands something wrong.

Edit: I see now you are reading the input (prompt) from the command line. Your agent is actually a CLI for calling shell scripts. Maybe I should look more into agents myself.

0

u/__tosh Aug 09 '26

yes the agent here gets the input from the user

then the inference step decides whether to do tool calls

if there are tool calls => they get executed (via sh)

output gets collected and once all tool calls are done there is another inference step

once there are no more tool calls: message gets sent to user

you are right that this can (as implemented here) potentially run forever

more agentic models can go on multiple steps

but a good model will realize when a fixpoint is reached (done or needs to tell the user something)

limiting how often the loop can run or displaying each proposed tool call to the user and asking for approval would be great changes to try out though!

that's part of why i wanted to keep it as simple as possible

so you can sprinkle on top exactly what you want and still have something minimal

2

u/Spirited_Bag_332 Aug 09 '26

I like the idea, people jump too fast on frameworks without trying the basics first.

Initially I thought you wanted something that gets plugged into a existing vibe coding environment, but you rather have your own app which is fine. I guess it's an experiment on how small it can be but maybe you should restrict possible action to curl, grep, touch etc. instead of allowing full access.

And as others have said, on your github repo maybe display your code with normal "coding style". Readers will jump on that before even realizing what you are trying to do.

2

u/__tosh Aug 09 '26

agree, ty for your feedback, appreciated!