loop-optimization

loop-optimization is a skill for Claude Code, Codex from zaryab2000/decipher-gas-optimizoor. It costs 103 tokens per session (1,807 once invoked), scanned A, original, MIT.

A Solidity review guide for finding gas waste in for and while loops, where repeated costs can add up across many iterations.

In plain words
What is it for?
Use it when writing or reviewing loops, auditing contract gas use, investigating costly functions, or checking Foundry gas reports.
Why use it?
It points out loop patterns that make each iteration more expensive, such as repeated storage reads or unnecessary overflow checks.

Skill for Claude CodeCodex

Part of the decipher-gas-optimizoor plugin — 11 skills, 5 commands, 1 agent shipped together

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.

agentmods
npx agentmods add skills/zaryab2000/decipher-gas-optimizoor/loop-optimization
Any agent
npx skills add zaryab2000/decipher-gas-optimizoor --skill loop-optimization
Clone the repo
git clone --depth 1 https://github.com/zaryab2000/decipher-gas-optimizoor

Made for: Claude Code, Codex.

Or install decipher-gas-optimizoor, the plugin that ships this one along with the rest of its 11 skills, 5 commands, 1 agent.

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 loop-optimization

README.md
[![agentmods](https://agentmods.dev/badge/skills/zaryab2000/decipher-gas-optimizoor/loop-optimization.svg)](https://agentmods.dev/skills/zaryab2000/decipher-gas-optimizoor/loop-optimization)
Your own site
<a href="https://agentmods.dev/skills/zaryab2000/decipher-gas-optimizoor/loop-optimization"><img src="https://agentmods.dev/badge/skills/zaryab2000/decipher-gas-optimizoor/loop-optimization.svg" alt="Measured on agentmods" height="20"></a>
Per session 103 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,807 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
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 $0.00103 $0.01807
Opus 5 $0.00051 $0.00903
Sonnet 5 $0.00021 $0.00361
Haiku 4.5 $0.00010 $0.00181

Measured 4d ago against content hash bdc7474e8969, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

loop-optimization 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 4d 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.

skills/loop-optimization/SKILL.md · 157 lines

How it starts

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

Purpose

Find and fix gas inefficiencies in Solidity loop constructs. Every per-iteration cost multiplies linearly — 100 iterations × 100 gas = 10,000 gas. Loop bodies are the highest-leverage location for gas optimization in Solidity contracts.

When to Use

  • Writing or reviewing any for or while loop in a Solidity contract
  • Auditing contracts for gas efficiency before deployment
  • Responding to gas reports showing high per-call cost on functions with loops
  • Reviewing contract code in Foundry-based projects (forge test --gas-report)

When NOT to Use

  • Memory arrays: .length on a memory array is an MLOAD — already cheap; no caching needed
  • Single-iteration loops: constant overhead savings (LO-003/LO-004) are not worth the noise
  • Loops with external calls that modify state: caching storage before such a loop risks stale values
  • Loops that modify the array length: never cache .length if the loop body calls push() or pop()
  • Solidity 0.8.22+ with simple counters: the compiler auto-skips overflow checks; LO-003 is redundant (but harmless — apply anyway for version-agnostic code)

Rationalizations to Reject

Claim Why it is wrong Correct action
"The loop is small" Per-iteration costs multiply linearly — 100 iterations × 100 gas = 10,000 gas Apply all loop optimizations regardless of loop size
"The compiler handles caching" Solidity does not cache storage reads across iterations; each loop iteration pays SLOAD cost Explicitly cache all loop-invariant storage reads before the loop
"It already has unchecked" unchecked on the counter does not cache the length or body storage reads Apply LO-001 + LO-002 + LO-003 together; they are independent savings

Platform Detection

# Confirm Foundry is available
forge --version

# Run gas report to find expensive loop functions
forge test --gas-report

# Trace SLOAD count in a specific loop function
forge test --match-test <testName> -vvvv | grep SLOAD

Read the full file on GitHub · 157 lines

Files

What ships with it

3 files beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

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. 4d ago First seen · 157 lines · 103 tokens per session scan A bdc7474e8969

Subscribe to this mod's changes

loop-optimization is a skill published in the GitHub repository zaryab2000/decipher-gas-optimizoor (7 stars, last pushed 6mo ago), licensed MIT. It adds 103 tokens to every session and 1,807 once invoked, about $0.0005 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.