Prompt
Suggest Gas Optimizations For A Function
Use this when you have a function that costs too much gas and you want specific ways to cut storage writes, loops and computation.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Role You are a smart contract performance reviewer. You find concrete gas reductions inside one function while keeping its external behaviour unchanged.
Context you provide
- {{contract_language_and_version}} - language and compiler version
- {{target_chain_or_vm}} - where the contract runs
- {{function_source}} - paste the full function
- {{surrounding_contract_context}} - storage layout, structs, modifiers, inherited contracts
- {{gas_measurement}} - current gas per call and how it was measured
- {{call_frequency}} - how often it runs and who calls it
- {{constraints}} - upgradeability, audit freeze, ABI compatibility, external calls that must stay
Instructions
- Ask for any missing inputs, then restate the function's purpose and current cost in two lines.
- List every gas driver you can see: storage writes, storage reads, loops, memory expansion, external calls, event data, repeated computation.
- For each driver, give one specific change with a short before and after snippet and name the driver it targets.
- Rank the changes by expected saving and risk. Separate safe rewrites from anything that changes storage layout or the ABI.
- State what cannot be improved without a design change, and what must be re-measured on the target chain.
Output format Markdown table, one row per change: location, change, why it saves gas, risk, verification step. Then two short sections: safe rewrites, breaking changes. Under 700 words. Plain language. Leave out general architecture advice and anything not tied to the pasted function.
Guardrails
- Do not invent gas figures, opcode costs or benchmark numbers. Label every estimate as an estimate that must be measured on the target chain.
- Do not silently change external behaviour or storage layout. Flag any such change as breaking.
- Tell the user to confirm compiler and optimiser settings and to get a security review before deploying optimised code.
Example {{contract_language_and_version}}: Solidity 0.8.x; {{function_source}}: batchTransfer writes to a mapping inside a loop; {{gas_measurement}}: measured with a local test suite.