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.
git clone --depth 1 https://github.com/knowledgestack/ks-cookbookWrote this? Show the measurements
A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.
[](https://agentmods.dev/rules/knowledgestack/ks-cookbook/python_clean_code)<a href="https://agentmods.dev/rules/knowledgestack/ks-cookbook/python_clean_code"><img src="https://agentmods.dev/badge/rules/knowledgestack/ks-cookbook/python_clean_code/github.svg" alt="Measured on agentmods" height="20"></a>Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.
<a href="https://agentmods.dev/rules/knowledgestack/ks-cookbook/python_clean_code"><img src="https://agentmods.dev/badge/rules/knowledgestack/ks-cookbook/python_clean_code.svg" alt="Reviewed on agentmods" width="80" height="20"></a>What 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.1 | $0.00009 | $0.00701 |
| Opus 5.5 | $0.00004 | $0.00280 |
| Sonnet 5.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 1mo ago.
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.
- 1mo ago 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 (9 stars, last pushed 3mo ago), licensed MIT. It adds 9 tokens to every session and 701 once invoked, about $0.0000 per session on Opus 5.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
fenic
Cursor rule "fenic" from typedef-ai/fenic, covering writing fenic, must-knows and traps fenic check can't catch — get these right by hand.
mcp-guidelines
You are an expert in Python, Model Context Protocol (MCP), and Atlan integration.
python
You are an AI assistant specialized in Python development. Your approach emphasizes.
hatch3r-python-patterns
Python 3.12+ conventions covering uv project management, Ruff lint+format, mypy strict typing, pytest parametrize, and the FastAPI/Django request-path + ORM N+1 floor.
go-conventions
Go conventions for all Go code (modules, naming, errors, logging, metrics).
secure-dev-python
These rules apply to all Python code in the repository and aim to prevent common security risks through disciplined use of input validation, output encoding, and safe APIs.