services
Get a key

library / How to use Solana / HS0058

Compute budget audit

One TypeScript file reads a program's newest settled transactions and prints, for each, the compute limit it requested, where that limit came from, the units it consumed and the share used. It only reads: no keypair, no signing.

By Carbium · Priority fees and compute · HS0058 · updated 2026-11-22

01

The default

A transaction that carries no compute-unit limit instruction still gets a limit. The figure 200,000 is an allotment per instruction. The runtime gives every instruction that is not a builtin 200,000 units, adds them across the message, and caps the sum at 1,400,000 per transaction. A swap with one program instruction starts at 200,000. A message with six program instructions starts at 1,200,000, and a message with eight starts at the 1,400,000 cap.

Builtin instructions, such as a system program transfer or the compute budget instructions themselves, draw a small allotment of their own and do not add 200,000. So 200,000 holds for the common one-instruction case, and a multiple of it applies when the message calls several programs.

$The limit is a ceiling on execution. A transaction that burns through it stops with an error and fails, and the fee is still charged.
02

The explicit limit and price

Two instructions in the compute budget program, ComputeBudget111111111111111111111111111111, change what the runtime would otherwise assume. Each carries a one byte tag followed by a little-endian number.

TagInstructionData after the tagUnit
2SetComputeUnitLimitu32compute units
3SetComputeUnitPriceu64micro-lamports per compute unit

A limit instruction replaces the default and is still capped at 1,400,000. A price instruction alone leaves the default limit in place and attaches a price to it. Sending the same compute budget instruction twice makes the transaction fail, so a message holds at most one limit and one price.

The priority fee is the requested limit times the price, divided by 1,000,000 and rounded up. It is computed from the limit in the message, whatever the transaction goes on to use. Priority fee per compute unit covers how to pick the price.

03

The code

Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24. The first argument is a program address and defaults to a busy swap program. The second is how many transactions to read, at most 10.

main.ts
// HS0058 · Compute budget audit// https://carbium.sh/library/howsolana/compute-budget-audit// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport {  address,  createDefaultRpcTransport,  createSolanaRpcFromTransport,  getBase58Encoder,} from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) {  console.error("Set CARBIUM_KEY to your Carbium API key.");  process.exit(1);}const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const rpc = createSolanaRpcFromTransport(  createDefaultRpcTransport({ url, headers: { "x-api-key": key } }),);// Usage: node main.ts [program-address] [count, at most 10]const [programArg = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4", countArg = "5"] = process.argv.slice(2);const count = Math.max(1, Math.min(Number(countArg) || 5, 10));const COMPUTE_BUDGET = "ComputeBudget111111111111111111111111111111";const BUILTIN = new Set([COMPUTE_BUDGET, "11111111111111111111111111111111"]);const PER_INSTRUCTION = 200_000;const MAX_LIMIT = 1_400_000;const MICRO = BigInt(1_000_000);const base58 = getBase58Encoder();// The json encoding names each program by index into accountKeys; data is base58.type Tx = {  transaction: { message: { accountKeys: string[]; instructions: { programIdIndex: number; data: string }[] } };  meta: { computeUnitsConsumed?: number | bigint } | null;};function audit(tx: Tx) {  if ("transactionConfig" in tx.transaction.message) throw new Error("v1 config"); // v1 declares its budget inline  const { accountKeys, instructions } = tx.transaction.message;  let limit: number | undefined;  let price = BigInt(0);  let programs = 0;  for (const ix of instructions) {    const program = accountKeys[ix.programIdIndex];    if (program !== COMPUTE_BUDGET) {      if (!BUILTIN.has(program)) programs++;      continue;    }    const data = Uint8Array.from(base58.encode(ix.data));    const view = new DataView(data.buffer);    if (data[0] === 2) limit = view.getUint32(1, true); // SetComputeUnitLimit: u32    if (data[0] === 3) price = view.getBigUint64(1, true); // SetComputeUnitPrice: u64  }  const source = limit === undefined ? `default ${programs} x 200000` : "instruction";  const requested = Math.min(limit ?? programs * PER_INSTRUCTION, MAX_LIMIT);  const used = Number(tx.meta?.computeUnitsConsumed ?? 0);  // The priority fee follows the requested limit, rounded up to a whole lamport.  const fee = (BigInt(requested) * price + MICRO - BigInt(1)) / MICRO;  return { requested, source, price, used, fee };}const sigs = await rpc.getSignaturesForAddress(address(programArg), { limit: 25 }).send();const landed = sigs.filter((s) => s.err === null).slice(0, count);const results = await Promise.allSettled(  landed.map((s) =>    rpc.getTransaction(s.signature, { encoding: "json", maxSupportedTransactionVersion: 1 }).send(),  ),);console.log(`${programArg.slice(0, 8)}: ${landed.length} settled transactions`);results.forEach((r, i) => {  const sig = landed[i].signature.slice(0, 8);  if (r.status === "rejected") return console.log(`${sig}  call failed: ${String(r.reason).slice(0, 80)}`);  if (r.value === null) return console.log(`${sig}  null (below the ledger floor)`);  try {    const a = audit(r.value as unknown as Tx);    const pct = a.requested ? ((a.used * 100) / a.requested).toFixed(1) : "0.0";    console.log(      `${sig}  requested ${a.requested} (${a.source})  price ${a.price}  consumed ${a.used}  ${pct}% used  fee ${a.fee} lamports`,    );  } catch {    console.log(`${sig}  message layout not readable`);  }});

run: node main.ts [program-address] [count]

The file lists the program's newest 25 signatures, drops the ones that failed and keeps the first five. It reads them at once with getTransaction and maxSupportedTransactionVersion: 1. A call that leaves commitment out runs at confirmed in @solana/kit, so each transaction is already settled.

At 50 cells a call for getTransaction and 50 for getSignaturesForAddress, five reads plus the listing cost 300 cells. That sits inside the 1,024 cell burst a rate class absorbs, so the run needs no pacing. Ten transactions cost 550.

With the json encoding, each instruction names its program as an index into accountKeys and carries its data as a base58 string. The audit decodes the string, reads tag 2 as a u32 and tag 3 as a u64, and counts the instructions whose program is not the system or compute budget program. Without a limit instruction, that count times 200,000 is the default, capped at 1,400,000. The consumed figure comes from meta.computeUnitsConsumed.

04

Reading requested against used

Each line gives the requested limit, its source (the word instruction, or default with the count and 200000), the price in micro-lamports, the units consumed, the percent used and the priority fee in lamports that the requested limit implies. The gap between the first and fourth numbers is what you pay for and never run.

Share usedWhat it tells you
Under 30 percentThe limit is generous. A default of 200,000 on a transaction that runs 40,000 lands here.
70 to 95 percentA limit set from a simulation with a margin. This is the shape to aim for.
Above 95 percentOne slow path in the program can push the transaction over its limit and fail it.

The cost shows up as soon as a price is attached. At 5,000 micro-lamports per unit, a limit of 1,400,000 carries a priority fee of 7,000 lamports, while a transaction that consumes 100,000 units needs a fee of 500. That is fourteen times the fee for the same work. The runtime does not refund the unused part.

A large limit also weighs on the leader's side. The leader reserves the requested limit when it schedules the transaction and settles to the units used after execution, so a padded request crowds scheduling while the block's final cost reflects actual use.

To tighten a limit, simulate the transaction, read the units it consumed and add a margin of your own. Compute unit limit from simulation walks through that step. For a decoded view of one transaction's instructions, see Transaction explainer.

05

Where it ends

Five transactions are a glimpse. The units a swap consumes change with the route, the pool and the account state, so one program can show 20 percent on one line and 60 on the next. Raise the count to 10 or run the file twice before you judge a limit.

The default shown is an estimate. The code counts every instruction outside the system and compute budget programs at 200,000. Another builtin, such as the upgradeable loader or the ed25519 and secp256k1 precompiles, gets about 3,000 from the runtime, and each system or compute budget instruction adds roughly 3,000 that the code leaves out. A line can therefore read high by close to 200,000 for each such builtin. When a line shows an instruction as the source, the figure is exact.

Only transactions that landed without error are read. A failed transaction often stopped at its limit, so the audit leaves out the lines that would show the worst fit. Both the requested limit and the consumed units cover the whole transaction including inner calls, so the audit cannot show which instruction used the units.

A signature listing comes from an address index that keeps up to 30 days and reaches below the validator's ledger floor, so an old signature can come back from getTransaction as null. The file prints that and moves on. It reads the compute budget instructions of legacy and v0 transactions. A v1 transaction declares its limit and priority fee in the message's transactionConfig and gets no 200,000 default, so the file prints it as not readable and does not guess.

06

When it goes wrong

what goes wrongwhat to do
A call fails with error -32015.The version is missing or 0 and the transaction is v1. Pass maxSupportedTransactionVersion: 1 in the config object of every getTransaction call.
A line says null for a signature the listing returned.The listing reaches below the validator's ledger floor, so an older signature may have no transaction behind it. Audit the newest signatures, which the file does by default.
Every line shows the default source and price 0.The program's callers send no compute budget instructions. Pass another program address or a larger count to see transactions that set them.
A transaction fails with a compute budget error after you trim its limit.The limit sat too close to the units consumed. Simulate again, read the highest figure across several runs and add a margin.
The transaction fails when it holds two limit instructions.A message takes one limit instruction and one price instruction. Build both once and remove any copy added by a helper.
07

Questions

Is the default compute limit 200,000?

It is 200,000 for each instruction that is not a builtin, added up across the message and capped at 1,400,000. A one-instruction transaction gets 200,000.

What is the most a transaction can request?

1,400,000 compute units. A limit instruction above that is capped at 1,400,000.

Do I get the priority fee back for units I did not use?

No. The priority fee follows the limit you request, so a limit close to your real usage is the way to cut it.

Do I need a private key to run this?

No. The file reads signatures and settled transactions. The Carbium key only identifies your account to the RPC endpoint.