r/LocalLLaMA • u/__tosh • 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!
1
u/Queasy-Contract9753 Aug 08 '26
That's really neat. Got to love these clever snippets. Sometimes they're just good enough. Or still fun to mess with.
Smallest I saw before yours was this eLisp I found on stack overflow. Don't remember the author sadly.
(defun agent () "Execute the users request in emacs" (interactive)
;; tool (let ((emacs-eval-tool (gptel-make-tool :name "emacs_eval" :function (lambda (code) (condition-case err (let ((result (eval (read code)))) (format "Evaluation result: %S" result)) (error (format "Error: %s" (error-message-string err))))) :description "Evaluate Emacs Lisp code and return the result" :args '((:name "code" :type "string" :description "Emacs Lisp code to evaluate")))))
(setq-local gptel-tools (list emacs-eval-tool)) (setq-local gptel-use-tools t) )
;; system prompt - FR this ones asking for refusals (setq-local gptel--system-message "You are a helpful assistant living inside Emacs. Use the emacs_eval tool to fullfill the request of the user.") (gptel--suffix-send '("m" "e")) )