A gas limit is the maximum amount of gas, the unit of computational work, that a transaction is allowed to consume, or that all transactions in a block may consume together. In smart contracts on Ethereum and other EVM-compatible chains, it serves as a safety ceiling on execution: it stops runaway code, bounds the fee a sender can pay and keeps every block small enough for nodes across crypto & blockchain networks to process in time.
The term covers two related settings. The transaction gas limit is chosen by the sender for a single transaction, while the block gas limit is a network-wide cap that validators adjust within protocol rules. Both are measured in gas units, not in currency.
How the gas limit caps smart contract execution
Every operation the Ethereum Virtual Machine executes has a fixed gas cost defined in the protocol: a simple addition costs 3 gas, writing a new value to storage costs about 20,000 gas, and a plain ether transfer costs a base of 21,000 gas. As a transaction runs, the EVM subtracts each cost from the gas limit the sender supplied.
If the remaining gas reaches zero before execution ends, the transaction fails with an out-of-gas error. All state changes are reverted, yet the gas already consumed is not refunded, because validators performed the work. If execution finishes early, the unused gas is returned, so the sender pays only for gas actually used.
This design solves the halting problem in practice. Nodes cannot predict whether arbitrary code will terminate, but a finite gas limit guarantees that every transaction stops after a bounded amount of computation, which protects the network from infinite loops and denial-of-service attacks.
Transaction gas limit and block gas limit
The two kinds of gas limit protect different parties and are set by different actors. The comparison below summarizes their roles.
| Property | Transaction gas limit | Block gas limit |
|---|---|---|
| Set by | The sender or wallet software | Validators within protocol bounds |
| Scope | One transaction | All transactions in one block |
| Purpose | Bounds execution and maximum fee | Bounds node workload and block size |
| Typical value | 21,000 for a transfer, far more for contracts | Tens of millions of gas on Ethereum mainnet |
| Effect if exceeded | Out-of-gas failure and revert | Transaction cannot be included in that block |
Since the London upgrade (EIP-1559), Ethereum uses a gas target of half the block gas limit. When blocks are fuller than the target, the base fee rises; when they are emptier, it falls. Validators can raise or lower the block gas limit only by a small fraction per block, so changes to network capacity happen gradually and require broad coordination.
Gas limit compared with gas price and gas fee
The gas limit is a quantity of work, whereas the gas price is the cost per unit. Confusing the two is one of the most common beginner mistakes, because wallets display them side by side. The total fee is the product of gas used and the effective price per gas.
| Concept | Unit | What it determines |
|---|---|---|
| Gas limit | Gas units | Maximum computation allowed |
| Gas used | Gas units | Computation actually performed |
| Base fee | Gwei per gas | Protocol price that is burned |
| Priority fee | Gwei per gas | Tip paid to the validator |
| Max fee per gas | Gwei per gas | Highest price the sender accepts |
| Gas fee | Ether (ETH) | Gas used multiplied by effective price |
The maximum possible fee of a transaction equals the gas limit multiplied by the max fee per gas. Wallets reserve this amount up front, which explains why a transaction can be rejected for insufficient funds even when the final fee would have been affordable.
Choosing and estimating the right gas limit
Wallets and libraries such as Ethers.js and Web3.js normally call eth_estimateGas, which simulates the transaction against the current state and returns the gas it would consume. Most tools then add a safety margin of roughly 10 to 30 percent, because state can change between estimation and inclusion.
Manual adjustment is sometimes justified, but it calls for care. The decision table below shows common situations and the recommended approach.
| Situation | Recommended approach | Reason |
|---|---|---|
| Simple ether transfer | Use exactly 21,000 | Cost is fixed by protocol |
| Token transfer or approval | Accept wallet estimate with margin | Cost varies slightly by token contract |
| Complex DeFi interaction | Add a larger margin of 20 to 30 percent | Execution path may depend on changing state |
| Contract deployment | Estimate in a test environment first | Bytecode size drives a high, variable cost |
| Estimation fails | Investigate the revert reason first | Failure usually signals a logic error, not low gas |
Developers working in Hardhat or Foundry can print gas reports for each function. These reports are the starting point for gas optimization, which reduces the gas a function needs and therefore the limit users must set.
Common mistakes and failure modes
Most problems with the gas limit stem from underestimation, unbounded loops or misplaced trust in estimates. The following issues appear repeatedly in audits and user support.
- Setting the limit too low: the transaction reverts and the consumed gas is lost, so the user pays for nothing.
- Unbounded loops over arrays: a function that iterates over a growing list eventually exceeds the block gas limit and becomes permanently unusable.
- Assuming a high limit raises cost: unused gas is refunded, so a generous limit increases only the reserved maximum, not the final fee.
- Hardcoding gas for external calls: forwarding a fixed amount, such as 2,300 gas, can break when opcode prices change in later upgrades.
- Ignoring state changes: an estimate made before another transaction lands can become too low by the time the transaction is mined.
The unbounded loop pattern deserves special attention, because it can create a gas-based denial of service. Auditors recommend pull-based payment patterns and pagination, so that no single call has to process an arbitrarily large data set.
In simple terms
Running a program on a blockchain costs effort, and every step of that effort is measured in units called gas. The gas limit is the most effort you agree to pay for. If the program needs more than that, it stops and its changes are undone.
It works like telling a taxi driver you will pay for at most 20 kilometers. If the trip is shorter, you pay only for the distance driven. If the destination is farther, the ride ends early and you still pay for the kilometers already traveled. The limit protects your wallet and keeps any single trip from blocking the road for everyone else.
A practical example
A user wants to swap a stablecoin for ether on a decentralized exchange. The wallet estimates 140,000 gas for the swap and proposes a gas limit of 170,000, about 20 percent higher. With a max fee of 30 gwei per gas, the wallet reserves 170,000 multiplied by 30 gwei, or 0.0051 ETH, as the worst-case fee.
Between estimation and inclusion, another trade shifts the pool and the swap follows a slightly longer route, consuming 152,000 gas. Because the margin covers this, the transaction succeeds. With an effective price of 24 gwei, the user pays 0.003648 ETH and the remaining reserved amount stays in the wallet. Had the user manually set the limit to 140,000, the swap would have failed with an out-of-gas error and the user would still have paid for the gas consumed.