How to Set Compute Units: Step by Step Guide.

Compute units? Yeah, they're basically the fuel for your blockchain transactions or AI jobs. If you're messing with Solana - which is what most folks mean these days - it's all about not getting your tx dropped because you ran out of gas. Or if you're in Qualcomm AI Hub land, it's picking which chip brains your model uses. But honestly, from what I see, you're probably here for Solana compute units. That's the hot one right now. Default's 200k per tx, but you can crank it up to 1.4 million max. Why? So your trades or swaps don't fail in a mempool bloodbath.

I usually start here because newbies waste SOL on failed txs without knowing this. Sound familiar? Let's fix that. We'll go step by step for Solana first - it's practical, saves you real money like ~0.000005 SOL per extra CU if you price it right. Then I'll hit Qualcomm and AMD quick, 'cause contexts matter.

Why bother setting compute units on Solana?

Transactions eat compute units (CUs) like candy. Too few? Boom, "out of compute" error. Network's congested? Your tx sits forever unless you pay priority fees tied to CUs. As of late 2025, blocks cap at 60 million CUs total, 1.4M per tx, 12M per account. Default 200k? Fine for simple transfers. But DeFi swaps? NFT mints? You're looking at 300k-800k easy.

The thing is, you simulate first to see exact usage. Add 10-20% margin. Then set a limit with SetComputeUnitLimit. Pair it with SetComputeUnitPrice for priority - that's microLamports per CU, like 10,000 means ~0.00001 SOL extra fee on a 400k CU tx. Pretty much pays validators to process you first.

Quick limits table

Limit TypeAmount
Per Transaction (max)1.4 million CU
Default per tx200,000 CU
Per Block60 million CU
Per Account per Block12 million CU

See? Don't guess. Simulate.

Step by step: Simulate and set CUs in Solana Web3.js (legacy - still everywhere)

  1. Grab your connection and instructions. Say you're doing a swap.

  2. Write a sim function. Here's mine - copy paste it.

    async function getSimulationUnits(connection, instructions, payer) { const testInstructions = [ ComputeBudgetProgram.setComputeUnitLimit({ units: 1400000 }), // Max for sim ..instructions ]; const tx = new Transaction().add(..testInstructions); tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash; tx.feePayer = payer; const simulation = await connection.simulateTransaction(tx, { sigVerify: false }); if (simulation.value.err) return undefined; return simulation.value.unitsConsumed;
    }
  3. Call it: const units = await getSimulationUnits(connection, yourInstructions, payerKey);

  4. Add margin. I do 10%: const adjusted = Math.ceil(units * 1.1); Caps at 1.4M obv.

  5. Build real tx. Add these first in instructions array:

    transaction.add( ComputeBudgetProgram.setComputeUnitLimit({ units: adjusted }), ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 10000 }) // Tune this
    );
    transaction.add(..yourInstructions);
  6. Sign, send. Watch it fly.

What's next? Test on devnet first. Costs nothing. In my experience, forgetting the recentBlockhash in sim kills it every time.

Solana Kit version (new hotness)

Okay, if you're on @solana/kit - faster, cleaner. Same logic.

  • Pipe your tx message.
  • Unshift limit/price instructions to front.
  • Sim with max limit instr included.
import { pipe, createTransactionMessage, appendTransactionMessageInstructions } from "@solana/kit";
import { getSetComputeUnitLimitInstruction, getSetComputeUnitPriceInstruction } from "@solana program/compute budget"; const limitIx = getSetComputeUnitLimitInstruction({ units: 1400000 });
const priceIx = getSetComputeUnitPriceInstruction({ microLamports: 10000 }); const testMsg = pipe( createTransactionMessage({ version: 0 }), (tx) => appendTransactionMessageInstructions([limitIx, ..instructions], tx) // .. set payer etc.
); // Sim it, get units, adjust, rebuild real msg with unshifted instrs.

Pro tip: For priority fees, hit an RPC like QuickNode's fetchEstimatePriorityFees. Grabs network congestion data. Set microLamports to their "high" rec - say 50k-200k in peak times. Saves failed tx retries.

Common screw ups and fixes

Tx fails with "Compute Budget Exceeded"? Your sim lied or no margin. Add 20% next time. Or sim failed - check for err in sim response.

Fees too high? Drop price to 0 - just sets limit, no priority. But good luck in congestion.

Max units but still drops? Account limit hit (12M/block). Wait a block or spread txs.

And simulation undefined? Instructions broken or bad payer. Log the sim.err - it's your friend.

Priority fees deep dive

No price set? Base fee only (~0.000005 SOL/tx). With price, it's price * CUs / 1e6 lamports. So 10k microLamports on 400k CUs = 4 SOL micro? Wait no:

Formula: total priority = (microLamportsPerCU * units) / 1000000 lamports. For 10,000 micro / 400k units = 4,000,000 microLamports = 0.004 SOL. Cheap insurance.

I usually start at 5k-20k. Bots run 100k+. Why does this matter? Friday pumps - network melts, low priority txs die.

Now, Qualcomm AI Hub - if that's your jam

Different beast. You're deploying ML models to edge devices. Compute units = CPU, GPU, NPU. Don't want your gaming app's AI hogging GPU? Set compute_unit=NPU,CPU.

Steps? Super simple.

  1. Pick job: compile, profile, inference.
  2. Add flag: qai hub job submit mobile_net.onnx --compute unit NPU Or comma list: --compute unit NPU,CPU
  3. Want fastest? --compute unit all or skip.
  4. Check dashboard. Profile job shows layers per unit. Visualize for colors/tensor shapes.

Example: Gaming app? GPU's for graphics, so NPU only. All 70 MobileNet layers hit NPU - boom, no overlap.

Issues? Syntax wrong - comma no spaces. Or unit unavailable on target device.

AMD ROCm - GPU hackers

Setting CUs on ROCm? Env vars. For compute workloads, not games.

  • export ROCGLOBALCU_MASK=0,2-4:0x337F GPU0 + GPUs2-4, specific CU masks.
  • Syntax wild: 0-3:0,5-10;4:0xFF
  • Sim queues only. HSACUMASK lower level.

Not all configs valid - WGPs pair CUs, can't half disable. Test or crash.

Wrap your head around it

Look, start with Solana if you're trading. Simulate every tx - it's 2 mins setup, saves hours debugging. I ran a bot last month, forgot margin once - ate 0.5 SOL in fails. Never again.

Qualcomm? Model devs, pick NPU for power sip. AMD? ML training tweaks.

Questions? "How much for my swap?" Sim it. Paste code if stuck. You'll get it quick.

One more: In code, always unshift CU instrs. Order matters - budget first. Gotcha city otherwise.