Getting it into your agent
One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.
npx agentmods add rules/knowledgestack/ks-cookbook/python_clean_codegit clone --depth 1 https://github.com/knowledgestack/ks-cookbookWhat it costs to keep this loaded
Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.
| Model | Per session | Once invoked |
|---|---|---|
| Fable 5 | $0.00009 | $0.00701 |
| Opus 5 | $0.00005 | $0.00351 |
| Sonnet 5 | $0.00002 | $0.00140 |
| Haiku 4.5 | $0.00001 | $0.00070 |
Grade A, and why
python_clean_code scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.
A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.
Nothing flagged
None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.
How it starts
The opening of the file, as written. The whole thing — 69 lines — stays where its author put it; the contents beside it link to each section on GitHub.
Clean-code rules for ks-cookbook
Applies to every Python file under flagships/, recipes/, and mcp-python/.
Hard constraints
SRP
- Each function does one thing. If its name or its docstring needs "and", split it.
- Each module has one concern.
agent.pywires the agent;schema.pyholds the output model;__main__.pyis CLI-only.
Small units
- Functions ≤ 30 lines.
- Modules ≤ 200 lines unless there is a clear reason.
- Recipes ≤ 100 LOC total (docstrings + comments don't count). Larger → promote to
flagships/.
Naming
- Intent-revealing. No
data,info,handler,managerunless domain-meaningful. - Boolean predicates:
is_*,has_*,should_*.
DRY & composition
- No copy-pasted agent wiring across flagships. Use the same
MCPServerStdioboilerplate; if you find yourself duplicating more than a few lines across files, factor into_shared/.
KISS
- No speculative abstractions. Three similar lines beats a premature helper.
Forbidden
Magic values
- No magic UUIDs or strings in code. Folder IDs belong in the Makefile as per-demo env defaults, not hardcoded in
agent.py.
Deep nesting
- Guard clauses and early returns. Max nesting depth 3.
"What" comments
- Only write comments that explain why (a hidden constraint, a workaround, a non-obvious tradeoff). Never comments that describe what a line does.
Error codes / sentinel returns
- Raise an exception or return a typed result. No
return Noneto signal "failure" alongside validNone.
God functions
- No function that orchestrates, does I/O, and transforms data. Split into pure helpers + a thin orchestrator.
Error handling
- Errors surface as exceptions, typed when possible.
- Never swallow an exception silently. If retrieval fails, say so in the output — don't emit made-up content.
- For MCP tool calls, let
MCPServerStdioerrors propagate. The CLI (__main__.py) catches and prints a useful message before exiting non-zero.
Style
What this file has done since we first saw it
Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.
- yesterday First seen · 69 lines · 9 tokens per session scan A 25e47d661772
python_clean_code is a cursor rule published in the GitHub repository knowledgestack/ks-cookbook (8 stars, last pushed 1mo ago), licensed MIT. It adds 9 tokens to every session and 701 once invoked, about $0.0000 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.
Other cursor rules, from other repositories
code-doc-lockstep
Binding — code changes must update matching docs (architecture / feature / API / runbook / SDD) in the same diff.
completion-time-self-audit
Self-audit before claiming work is complete (no defer / follow-up / mock).
vk-org-resolution
VirtualKey org resolution has TWO join chains (Project for application VKs, Owner for personal VKs); vkSelectSQL must COALESCE both or personal VKs silently return NULL org.
design-tokens
Visual values via CSS variables; no hex / rgb literals in style.
i18n-mandatory
All user-visible UI text uses t('namespace:section.key') from react-i18next.
useapi-querykey
Every useApi(fetcher, queryKey) call must start queryKey with at least two string literals that uniquely identify the endpoint, followed by any state variables that affect the fetcher.