# Callback Gas Limit Estimation

When interacting with the AI Oracle, it is crucial to allocate sufficient gas for the execution of the callback function. The gas consumed during the callback depends entirely on the implementation of the `aiOracleCallback` method. Its logic directly impacts gas usage - the more complex the logic, the higher the gas consumption. Carefully design your `aiOracleCallback` to balance functionality and gas efficiency.

To estimate the gas required for the `aiOracleCallback` in a Foundry project:

1. Run the following command to generate a gas report:

   ```bash
   forge snapshot --gas-report
   ```
2. Locate your **Prompt** contract in the report and check the gas usage for the `aiOracleCallback` method.
3. Use the calculated gas amount to set the gas limit during the initialisation of the Prompt contract (in the constructor).

Alternatively you can use other tools like Hardhat or Tenderly.

### Test Script

* We provided [test script](https://github.com/ora-io/Interaction_With_OAO_Template/blob/main/test/EstimateGasLimit.t.sol) for gas limit estimation in the template repository.
* To execute estimation run: ***forge test test/EstimateGasLimit.t.sol -vvvv*** . Next to the callback function, you can observe the gas amount needed for execution.\
  \
  eg. \[<mark style="color:green;">1657555</mark>] Prompt::aiOracleCallback -> in this case amount we can set gas limit to <mark style="color:green;">1700000</mark>

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console2, Vm} from "forge-std/Test.sol";
import {Prompt} from "../src/Prompt.sol";
import {IAIOracle} from "OAO/contracts/interfaces/IAIOracle.sol";
import {OraSepoliaAddresses} from "./OraSepoliaAddresses.t.sol";
import "forge-std/console.sol";

contract EstimateGasLimitTest is Test, OraSepoliaAddresses {
    Prompt prompt;
    string rpc;
    uint256 forkId;
    uint256 modelId;
    string result;

    function setUp() public {
        rpc = vm.envString("RPC_URL");
        forkId = vm.createSelectFork(rpc);
        prompt = new Prompt(IAIOracle(OAO_PROXY));
        modelId = 50;
        result = "Qmd3xWJVRao8AfgRTuXLCWBYj6VdVV8HFuKygk9FLTW5bi";
    }

    function test_estimateGasLimit() public {
        uint256 requestId = prompt.calculateAIResult{value: prompt.estimateFee(modelId)}(modelId, "test generation");
        uint256 before = gasleft();
        vm.prank(OAO_PROXY);
        prompt.aiOracleCallback(requestId, bytes(result), "");
        uint256 afterCall = gasleft();
        console.log(before - afterCall);
    }
}
```

The above script estimates gas necessary for Stable-Diffusion callback. Note that result will always be constant size (ipfs CID).

#### **Llama3**&#x20;

To estimate gas limit for Llama3, you need to change **modelId** and **result**.&#x20;

```solidity
modelId = 11;
result = "Beneath the sky, where whispers blend, A story stirs that has no end. The trees bow low, the rivers hum, A timeless tune, where life has sprung. The sun ascends with golden rays, To bless the fields and mark the days. The flowers bloom, their colors speak, Of beauty vast, profound, unique. The stars alight, a cosmic sea, Of dreams untold and destiny. Each twinkle holds a secret dear, A tale of love, of hope, of fear. Through valleys deep and mountains wide, The journey calls, the hearts true guide. With every step, a lesson found, In fleeting moments, wisdoms ground. Yet in the shadows, doubts may creep, A restless thought, a troubled sleep. But courage whispers, soft and clear, Youre not alone; Im always near. So carry forth, embrace the storm, Through fire and frost, the heart is warm. For lifes a dance, both wild and free, A fleeting song of harmony. As time unfurls its endless thread, The soul will soar where dreams are led. A symphony of lifes embrace, endless journey through boundless space. Beneath the moon, the rivers gleam, A quiet whisper shapes a dream. Through winds of change and skies of blue, I love you.";
```

💡 The maximum length of Llama3 response is 200 words string, hence we will use this string size to estimate gas limit.
