capture-state-closure

capture-state-closure is a skill for Claude Code from jimmc414/claude-code-plugin-marketplace. It costs 27 tokens per session (591 once invoked), scanned A, original, MIT.

A function that remembers values from the surrounding function that created it. This is called a closure and can hold simple persistent state without defining a class.

In plain words
What is it for?
Use it for counters, callback factories, simple stateful functions, and memoization with a cache that can be inspected.
Why use it?
It provides a compact way to create specialized functions that retain context between calls. It avoids a class when only one callable and a small amount of state are needed.

Skill for Claude Code

Written for Claude Code: shipped in a Claude Code plugin.

Part of the norvig-patterns plugin — 54 skills shipped together

Good fit Use it for counters, callback factories, simple stateful functions, and memoization with a cache that can be inspected.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/jimmc414/claude-code-plugin-marketplace/capture-state-closure
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.

Any agent
npx skills add jimmc414/claude-code-plugin-marketplace --skill capture-state-closure
Clone the repo
git clone --depth 1 https://github.com/jimmc414/claude-code-plugin-marketplace

Made for: Claude Code.

Or install norvig-patterns, the plugin that ships this one along with the rest of its 54 skills.

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 capture-state-closure

README.md
[![agentmods](https://agentmods.dev/badge/skills/jimmc414/claude-code-plugin-marketplace/capture-state-closure/github.svg)](https://agentmods.dev/skills/jimmc414/claude-code-plugin-marketplace/capture-state-closure)
Your own site
<a href="https://agentmods.dev/skills/jimmc414/claude-code-plugin-marketplace/capture-state-closure"><img src="https://agentmods.dev/badge/skills/jimmc414/claude-code-plugin-marketplace/capture-state-closure/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 capture-state-closure

Your own site · 80×15
<a href="https://agentmods.dev/skills/jimmc414/claude-code-plugin-marketplace/capture-state-closure"><img src="https://agentmods.dev/badge/skills/jimmc414/claude-code-plugin-marketplace/capture-state-closure.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 27 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 591 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.00027 $0.00591
Opus 5 $0.00014 $0.00296
Sonnet 5 $0.00005 $0.00118
Haiku 4.5 $0.00003 $0.00059

Measured 9d ago against content hash bc012889bedf, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-09, from the pricing page.

Security

Grade A, and why

capture-state-closure 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 9d 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.

plugins/norvig-patterns/skills/capture-state-closure/SKILL.md · 95 lines

How it starts

The opening of the file, as written. The whole thing — 95 lines — stays where its author put it; the contents beside it link to each section on GitHub.

capture-state-closure

When to Use

  • Need state that persists across function calls
  • Simpler than defining a class
  • Creating specialized functions from a template
  • Memoization with accessible cache
  • Callback factories

When NOT to Use

  • Multiple methods needed (use class)
  • State is complex (use class)
  • Need inheritance or protocols

The Pattern

Nested function captures variables from enclosing scope.

def make_counter(start=0):
    """Create a counter function with persistent state."""
    count = start  # Captured by inner function

    def counter():
        nonlocal count
        count += 1
        return count

    return counter

c = make_counter(10)
c()  # 11
c()  # 12

Example (from pytudes)

# Memoization via closure (ngrams.py)
def memo(f):
    """Memoize function f."""
    table = {}  # Captured mutable state

    def fmemo(*args):
        if args not in table:
            table[args] = f(*args)
        return table[args]

    fmemo.memo = table  # Expose cache for inspection
    return fmemo

@memo
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

# Access the cache
fibonacci.memo  # {(0,): 0, (1,): 1, (2,): 1, ...}

# Symbol interning (lispy.py)
def Sym(s, symbol_table={}):  # Mutable default = persistent state
    """Find or create unique Symbol for string s."""
    if s not in symbol_table:
        symbol_table[s] = Symbol(s)
    return symbol_table[s]

# Same symbol every time
assert Sym('quote') is Sym('quote')

# Partial application via closure
def make_multiplier(factor):
    """Create a function that multiplies by factor."""
    def multiply(x):
        return x * factor  # factor captured
    return multiply

double = make_multiplier(2)
triple = make_multiplier(3)
double(5)  # 10
triple(5)  # 15

Key Principles

  1. Inner function sees outer scope: Variables are captured
  2. Mutable objects shared: Lists, dicts can be modified
  3. nonlocal for rebinding: Needed to reassign immutable captured vars
  4. Default args persist: Mutable default arg is like static variable
  5. Expose internals: Attach captured state as attribute for inspection

Read the full file on GitHub · 95 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. 9d ago First seen · 95 lines · 27 tokens per session scan A bc012889bedf

Subscribe to this mod's changes

capture-state-closure is a skill published in the GitHub repository jimmc414/claude-code-plugin-marketplace (4 stars, last pushed yesterday), licensed MIT. It adds 27 tokens to every session and 591 once invoked, about $0.0001 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.

Related

Other skills, from other repositories

audit-progressive-disclosure

Read-only progressive-disclosure audit for agent-facing instruction markdown. Grades every target against a three-tier load-cost model (always-loaded / invocation-loaded / on-demand) and classifies seven finding shapes in two lanes: split opportunities (oversize vs tier-calibrated Anthropic-prescribed caps…

melodic-software/claude-code-plugins · 259 tokens

quiz-me

Post-work comprehension check: after a change is complete, generate a self-contained HTML report of what was done (context, intuition, decisions) with a quiz at the bottom that you answer. Verifying the HUMAN absorbed the work, not the artifact. Non-gating by default; the quizpolicy userConfig tunes offer cadence.…

melodic-software/claude-code-plugins · 177 tokens

reach

Run a Claude Code agent turn on ANOTHER machine in the fleet, over SSH on the tailnet. Every machine signs into its own Claude account, so the peer tools (ListAgents, SendMessage) are same-account and never span machines; SSH plus a headless claude -p is the path that does. Carries: resolving a target host from…

melodic-software/claude-code-plugins · 226 tokens

shape

Shape the assistant's output for a reader with ADHD, and anyone who wants action-first, low-friction responses. Lead with the concrete next action, number multi-step work, restate state across turns, cap and rank lists, give concrete time estimates, make wins visible, and cut preamble, recap, and closers. Use when…

melodic-software/claude-code-plugins · 209 tokens

generate

Build a source-backed AI industry briefing from official vendor publications, configured RSS feeds, GitHub releases, reputable secondary reporting, and user-supplied URLs. Use when: 'ai briefing', 'ai news', 'what's new in AI', 'catch me up on AI', 'prep for AI meeting', 'AI roundup', or 'generate AI slides'.

melodic-software/claude-code-plugins · 72 tokens

changelog

Ingest Claude Code changelog entries and integrate them into the current repo. Fetch (read-only display), diff (impact analysis, no edits), status (applied versions), and apply (full integrate pipeline, explicit user intent only). Use when: 'new cc version', 'what changed in claude code', 'apply changelog', a new CC…

melodic-software/claude-code-plugins · 84 tokens