python_clean_code

python_clean_code is a cursor rule for Cursor from knowledgestack/ks-cookbook. It costs 9 tokens per session (701 once invoked), scanned A, original, MIT.

A set of Python style rules for a cookbook project’s flagship demos, recipes, and MCP Python files. It defines how small functions, focused modules, names, shared wiring, and configuration values should be handled.

In plain words
What is it for?
Use it while writing or reviewing Python files under `flagships/`, `recipes/`, and `mcp-python/`, including agent wiring, schemas, command-line entry points, and recipe size decisions.
Why use it?
It prevents examples from becoming difficult to read, duplicated, deeply nested, or overloaded with unrelated responsibilities. It also sets clear size and naming limits for keeping the code maintainable.

Cursor rule for Cursor

Written for Cursor: installed under .cursor/.

Good fit Use it while writing or reviewing Python files under flagships/, recipes/, and mcp-python/, including agent wiring, schemas, command-line entry points, and recipe size decisions.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/knowledgestack/ks-cookbook/python_clean_code
Install

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.

Clone the repo
git clone --depth 1 https://github.com/knowledgestack/ks-cookbook

Made for: Cursor.

Wrote 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.

agentmods badge for python_clean_code

README.md
[![agentmods](https://agentmods.dev/badge/rules/knowledgestack/ks-cookbook/python_clean_code/github.svg)](https://agentmods.dev/rules/knowledgestack/ks-cookbook/python_clean_code)
Your own site
<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.

agentmods 80×15 button for python_clean_code

Your own site · 80×15
<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>
Per session 9 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 701 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

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.

ModelPer sessionOnce 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

Measured 1mo ago against content hash 25e47d661772, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

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.

.cursor/rules/python_clean_code.mdc · 69 lines

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.py wires the agent; schema.py holds the output model; __main__.py is 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, manager unless domain-meaningful.
  • Boolean predicates: is_*, has_*, should_*.

DRY & composition

  • No copy-pasted agent wiring across flagships. Use the same MCPServerStdio boilerplate; 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 None to signal "failure" alongside valid None.

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 MCPServerStdio errors propagate. The CLI (__main__.py) catches and prints a useful message before exiting non-zero.

Style

Read the full file on GitHub · 69 lines

Changes

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.

  1. 1mo ago First seen · 69 lines · 9 tokens per session scan A 25e47d661772

Subscribe to this mod's changes

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.