How to set a VRF callback gas limit
Set callbackGasLimit to the most gas your callback can need, measured from cold storage, plus about 20%. Measure it with the Foundry test below, not by guessing: the six callbacks we measured needed between 26,605 and 121,375 gas, and the cost came from storage written for the first time, not from the random math. Whether the extra 20% costs you anything depends on who fulfills the request. Chainlink VRF v2.5's wrapper and Pyth Entropy price the limit you reserve. Randoo, native Ronin VRF and Chainlink subscriptions bill the gas the fulfillment used.
What the limit covers
The coordinator calls your callback with exactly callbackGasLimit gas. If the callback needs more, it runs out of gas and your game state does not change, while the request counts as fulfilled on Randoo, Chainlink VRF and native Ronin VRF. When a VRF callback reverts shows what each provider does then and how to finish the outcome from the stored seed.
The limit is for your callback alone. Proof verification and payment run outside it, and every Randoo quote adds 500,000 gas for them (Fees and failures). On Randoo's Ronin and Chainlink wrapper interfaces the limit is 50,000 to 2,500,000, and a request outside that range reverts with GasLimit. The D20DAO interface accepts 30,000 to 2,500,000 and charges anything below 50,000 as 50,000.
Where a callback's gas goes
The callback runs in a later transaction than the request, so every storage slot and contract it touches is cold the first time. These are the EVM costs that decide most callbacks (EIP-2929, EIP-2200):
| Operation | Gas |
|---|---|
| First read of a storage slot | 2,100 |
| First write to a slot that holds zero | 22,100 |
| First write to a slot that holds a nonzero value | 5,000 |
| First call to, or balance read of, another address | 2,600 |
| Sending value with a call | 9,000 more, and 25,000 more if the receiving account is empty |
| An event | 375, plus 375 per indexed topic and 8 per byte of data |
Hashing a seed into a roll costs tens of gas: each extra die in Several VRF requests at once adds about 370 gas. Writing a new player's result to an empty slot costs 22,100, sixty times as much. Storage refunds for clearing a slot are paid at the end of the transaction, so they never give the callback more gas to run with.
Measure your callback
Put this file next to the template test from the integration playbook, at test/templates/CallbackGas.t.sol, with the same five files laid out the same way. It requests through the mock coordinator, then finds by binary search the least gas at which the coordinator's call into rawFulfillRandomSeed succeeds, once per seed, and returns to the same state before each try.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// Finds the least gas your callback needs, from cold storage, over many seeds.
// Sits next to RandooConsumerTemplate.t.sol and uses the same mock and layout.
import {Test, console} from "forge-std/Test.sol";
import {IRoninVRFConsumer} from "../../src/interfaces/IRoninVRFCoordinatorForConsumers.sol";
import {MockRandooCoordinator} from "./MockRandooCoordinator.sol";
import {TemplateConsumer} from "./TemplateConsumer.sol";
contract CallbackGasTest is Test {
/// The callbackGasLimit your contract sends with each request.
uint256 internal constant CALLBACK_GAS = 50_000;
/// Seeds to try. Raise it when the callback's cost depends on the seed.
uint256 internal constant SEEDS = 100;
MockRandooCoordinator internal coordinator;
// ---- SWAP POINT: your consumer contract. ----
TemplateConsumer internal consumer;
function setUp() public {
coordinator = new MockRandooCoordinator();
// ---- SWAP POINT: construct your consumer. ----
consumer = new TemplateConsumer(address(coordinator));
}
function test_callbackGasHasHeadroom() public {
uint256 worst;
for (uint256 i; i < SEEDS; i++) {
uint256 before = vm.snapshotState();
// ---- SWAP POINT: make the most expensive request, e.g. a new player's first. ----
bytes32 reqHash = consumer.roll{value: 1 ether}(CALLBACK_GAS, 21 gwei, address(this));
uint256 needed = _leastGas(reqHash, uint256(keccak256(abi.encode("seed", i))));
if (needed > worst) worst = needed;
vm.revertToState(before);
}
console.log("worst case callback gas:", worst);
assertGe(CALLBACK_GAS, worst * 12 / 10, "less than 20% headroom");
}
/// Binary search for the least gas at which the coordinator's call succeeds.
function _leastGas(bytes32 reqHash, uint256 seed) internal returns (uint256) {
uint256 requested = vm.snapshotState();
uint256 lo = 0;
uint256 hi = coordinator.MAX_CALLBACK_GAS();
require(_deliver(reqHash, seed, hi), "callback reverts with any gas");
while (hi - lo > 1) {
uint256 mid = (lo + hi) / 2;
vm.revertToState(requested);
if (_deliver(reqHash, seed, mid)) hi = mid;
else lo = mid;
}
vm.revertToState(requested);
return hi;
}
function _deliver(bytes32 reqHash, uint256 seed, uint256 gas) internal returns (bool ok) {
// The request warmed the consumer's storage. On chain the fulfillment is a later
// transaction that starts cold, so cool it, and every other contract or account
// the callback touches, or the result comes out too low.
vm.cool(address(consumer));
vm.prank(address(coordinator));
(ok,) = address(consumer).call{gas: gas}(
abi.encodeCall(IRoninVRFConsumer.rawFulfillRandomSeed, (reqHash, seed))
);
}
}Run forge test --match-contract CallbackGasTest -vv. It prints the worst case and fails if CALLBACK_GAS leaves less than 20% over it. The template consumer, which stores the seed and the request hash, needs 26,605 gas, so its 50,000 passes.
To measure your own contract, change the three swap points. The request is the one that matters: set up the most expensive case before it, such as a new player whose counters are all zero, the largest raffle your contract allows, or the bet that wins and pays out. If the callback sends value, vm.cool the receiving address in _deliver too. A callback that fails even at 2,500,000 gas stops the test with callback reverts with any gas: that is a bug in the callback, not a gas problem.
The test searches for the least gas that succeeds instead of reading gasleft() around a call, because that is the number the limit has to cover. A contract that makes its own calls needs more than it uses: a call can pass on at most 63/64 of the gas left.
For a Chainlink VRF v2.5 wrapper consumer, prank as the wrapper and call rawFulfillRandomWords(requestId, words) in _deliver. For a D20DAO consumer, prank as the coordinator and call rawFulfillRandomness(requestId, randomness). The search stays the same.
Measured callbacks
Each worst case is the least gas at which the coordinator's call succeeded, from cold storage, over the seeds and states named in each guide:
| Contract | What the callback does | Worst case, Solidity 0.8.38, via-IR, 200 runs | Worst case, 0.8.30, no optimizer | Limit the guide sets |
|---|---|---|---|---|
| Quickstart template | Stores the seed and the request hash | 26,605 | 27,101 | 50,000 |
| NFT reveal | Reads three slots, writes one offset | 28,434 | 30,077 | 50,000 |
| Dice game, won bet | Pays the player | 26,727 | 29,437 | 100,000 |
| Dice game, wallet refuses the payout | Credits the winnings: two new slots | 68,970 | 72,020 | 100,000 |
| Several requests at once, ten dice | Ten rolls, one new result slot | 35,867 | 44,718 | 60,000 |
| Raffle, 16,000,000 entries | Binary search over running totals, writes the winner | 93,199 | 106,761 | 120,000 |
| Loot box, new player | Three weighted rolls, a counter per new rarity | 121,375 | 124,751 | 150,000 |
What these show:
- First-time writes set the cost. The loot box is the most expensive because a new player's first box writes a flag and up to three counters that were zero. The dice game's refused payout costs more than double the paid one for the same reason.
- Loops grow slowly. The raffle's binary search adds about 7,000 to 10,000 gas each time the number of entries grows tenfold. A loop that writes storage on every pass grows far faster: cap its length, as
Rollscaps a roll at ten dice. - The compiler setting matters by 2 to 25%. Measure with the settings you deploy with.
- Sample values are not measurements. Chainlink's getting-started guide uses
callbackGasLimit = 40000and its best practices page uses100000. The loot box would fail with either.
What headroom costs on each provider
| Provider | What a request pays for | Gas reserved but not used |
|---|---|---|
| Randoo, all three interfaces | A prepayment quoted from your limit. The coordinator keeps the service fee and the gas the fulfillment used (Fees and failures) | Refunded at fulfillment, to the refund address (on the Chainlink wrapper interface, to the consumer if it accepts ETH) |
| Native Ronin VRF | A prepayment quoted from your limit, the same gas formula as Randoo's (VRF on Ronin) | The coordinator pays the oracle's gas and refunds the rest |
| Chainlink VRF v2.5, subscription | The gas the fulfillment used, plus a premium, charged to the subscription: startGas - gasleft() in VRFCoordinatorV2_5.sol (@chainlink/contracts 1.5.0) | Never charged |
| Chainlink VRF v2.5 wrapper (direct funding) | A price computed at request time from your limit, the wrapper's and coordinator's overhead, and a premium (_calculateRequestPriceNative in VRFV2PlusWrapper.sol) | Kept. Chainlink's billing page: "your contract is not refunded for the excess gas amount that you paid" |
| Pyth Entropy | getFeeV2(gasLimit). On Base at block 56,814,657 it was 0.000015 ETH at every limit from 100,000 to 500,000 and 0.00002 ETH at 1,000,000 (Migrate from Pyth Entropy) | Not refunded |
| D20DAO | On Robinhood, the larger of 0.000025 ETH and 2 × base fee × (405,000 + your callback gas) (Randoo vs D20DAO) | Priced in: the fee is quoted from your limit |
So on Chainlink's wrapper every gas of headroom is paid for, and on Pyth Entropy only above a fee step. Size the limit close to the measured worst case there, and measure again whenever the callback changes.
On Randoo, headroom costs nothing beyond a larger prepayment that comes back when the request is fulfilled. Of the 200 latest requests on Randoo's Ronin coordinator (2026-11-11 to 2026-11-19), 124 reserved 900,000 gas, and the gas Randoo kept per request had a median of 0.0068 RON and a maximum of 0.0241 RON. A Chainlink wrapper consumer moving to Randoo keeps its callbackGasLimit: on Randoo's wrapper interface the callback gets exactly that much, as it did on Chainlink's (Migrate from Chainlink VRF v2.5).
When the limit is wrong
- Too low. The callback runs out of gas and the seed is delivered anyway. Give the contract a public
settlethat finishes the outcome from the stored seed, as every Randoo guide does (When a VRF callback reverts). - Too high on Randoo. Each extra unit of gas adds the buffered gas price to the prepayment (the declared price plus 10% on Ronin, 25% on Base and Robinhood), and the unused part comes back. Above 2,500,000 the request reverts with
GasLimit. - Fine in tests, too low in production. The cost depends on state the test did not reach: a bigger collection, a new kind of player, a payout to a contract wallet. Measure at the largest input your contract accepts, or cap the input.
Tests we ran
With Foundry 1.9.0 and forge-std, on 2026-11-24:
- The test above passes against the quickstart template: worst case 26,605 gas with Solidity 0.8.38, via-IR and 200 optimizer runs, and 27,101 with 0.8.30 without the optimizer, over 100 seeds.
- Pointed at
Rollsfrom Several VRF requests at once, each request from a new player, it reproduces that guide's table: 32,507 gas for one die and 35,867 for ten with 0.8.38, and 35,661 and 44,718 with 0.8.30.
Updated 2026-11-24.