Sub topic 11
Bitcoin Fees: Fee Rate Times Transaction Size
This chapter assumes you know what a UTXO is (an unspent transaction output) and that a Bitcoin transaction spends inputs and creates outputs. It also assumes you are comfortable with the unit conversion 1 BTC = 100,000,000 satoshis (sats). The Python snippets are short and optional.
The Core Formula
A Bitcoin transaction fee is not a percentage of the amount sent. It is the size of the transaction multiplied by a price per unit of size:
fee (sats) = virtual size (vB) × fee rate (sat/vB)Sending 0.001 BTC and sending 50 BTC cost the same if the two transactions have the same inputs and outputs. The fee depends on how much block space the transaction uses, not on how much value moves.
The fee is never written into the transaction. Nodes compute it as the sum of input values minus the sum of output values. Whatever you do not assign to an output goes to the miner. This leads to a common error: forgetting the change output. If you spend a 1 BTC input and create only a 0.1 BTC output, the miner receives 0.9 BTC. Wallet software guards against this, but hand-built raw transactions do not.
Weight, Virtual Size, and Why SegWit Is Cheaper
Block space is limited to 4,000,000 weight units (WU). Each byte of a transaction counts as 1 to 4 WU:
- Non-witness data (version, input references, outputs, locktime) counts 4 WU per byte.
- Witness data (signatures and scripts in SegWit inputs) counts 1 WU per byte.
Virtual size is weight divided by 4, rounded up:
vsize (vB) = ceil(weight / 4)A transaction of 562 WU has a vsize of 141 vB. Because witness bytes are discounted, an input that moves its signature into the witness is cheaper than the same input in the legacy format. The fee rate is quoted per vB, not per raw byte. Mixing up sat/B and sat/vB is a common error. For a SegWit transaction the raw byte size is larger than the vsize, so a tool that uses raw bytes will overstate the fee.
Estimating Size from Inputs and Outputs
Size grows with the number of inputs and outputs and with the script type of each. The approximate costs are:
| Script type | Input (vB) | Output (vB) | Fixed overhead (vB) |
|---|---|---|---|
| P2PKH (legacy) | 148 | 34 | 10 |
| P2SH-P2WPKH (wrapped SegWit) | 91 | 32 | 10.5 |
| P2WPKH (native SegWit) | 68 | 31 | 10.5 |
| P2TR (Taproot, key path) | 57.5 | 43 | 10.5 |
These are typical values. Real sizes vary by a byte or two depending on signature encoding, and multisig or script-path spends are much larger. The following code estimates a fee from the table:
import math
# script type: (input vB, output vB, fixed overhead vB)
SIZES = {
"p2pkh": (148, 34, 10),
"p2sh-p2wpkh": (91, 32, 10.5),
"p2wpkh": (68, 31, 10.5),
"p2tr": (57.5, 43, 10.5),
}
def estimate_vsize(script, n_in, n_out):
inp, out, overhead = SIZES[script]
return math.ceil(overhead + n_in * inp + n_out * out)
def fee_sats(vsize, rate_sat_per_vb):
return vsize * rate_sat_per_vb
for script in SIZES:
size = estimate_vsize(script, 1, 2)
print(script, size, fee_sats(size, 10))For a one-input, two-output transaction at 10 sat/vB, this prints 226 vB and 2,260 sats for P2PKH, 166 vB and 1,660 sats for wrapped SegWit, 141 vB and 1,410 sats for native SegWit, and 154 vB and 1,540 sats for Taproot. Native SegWit saves about 38% over legacy for the same payment.
Taproot is cheaper per input but costlier per output. It pays off most for wallets that spend many inputs, and less for a simple one-input payment.
The sizes in the table come from the formula in the diagram below.
Where the Fee Rate Comes From
Miners fill each block with the transactions that pay the highest fee rate first. The fee rate you need therefore depends on how many other transactions are waiting in the mempool (the pool of unconfirmed transactions). The chapter on fee markets covers the auction logic. For this chapter the practical points are:
- Wallets ask a node for an estimate. Bitcoin Core's
estimatesmartfeetakes a target number of blocks and returns a rate in BTC per kvB, so convert before comparing it with sat/vB figures. - A 1-block target costs more than a 6-block or 144-block target. If you can wait, choose a longer target.
- Fee rates swing widely. A rate that confirms in one block on a quiet weekend can stall for days during a congested period.
- Nodes refuse to relay transactions below a minimum relay rate, which is 1 sat/vB by default. Outputs below the dust threshold, roughly 294 sats for P2WPKH and 546 sats for legacy outputs, are also rejected.
A concrete example: at 10 sat/vB, a 141 vB transaction costs 1,410 sats. At a hypothetical price of $60,000 per BTC, that is 0.0000141 BTC, or about $0.85. At 100 sat/vB the same transaction costs about $8.46.
Why Many Small Inputs Hurt
Each input adds 68 vB for native SegWit, so a wallet holding many small UTXOs pays more to spend them than a wallet holding one large UTXO. Consider sending a payment funded by 10 inputs:
size = estimate_vsize("p2wpkh", 10, 1) # 722 vB
print(size, fee_sats(size, 5), fee_sats(size, 50)) # 722 3610 36100Consolidation means merging many small UTXOs into one when fees are low. It costs a fee now (3,610 sats at 5 sat/vB) to avoid a larger fee later (36,100 sats at 50 sat/vB). A UTXO is also uneconomic when its value is below the fee to spend it. At 50 sat/vB, a P2WPKH input costs 3,400 sats to spend, so any UTXO worth less than that is effectively stuck until fees fall.
Batching works on the same principle. Paying 10 recipients in one transaction adds 31 vB per extra output. Ten separate payments would each pay the 10.5 vB overhead and a 68 vB input. Exchanges batch withdrawals for this reason, which is one source of the gap discussed in the withdrawal fees chapter.
Fixing a Stuck Transaction
If you set the rate too low, the transaction waits in the mempool. There are two standard fixes, and each depends on who controls what.
- Replace-by-fee (RBF). The sender broadcasts a replacement that spends the same inputs with a higher fee. The original must have signaled replaceability, which is the default in most current wallets. The replacement must pay a higher absolute fee than the original, and its rate must exceed the old rate by at least the incremental relay rate (1 sat/vB by default).
- Child-pays-for-parent (CPFP). The recipient, or the sender through the change output, spends an output of the stuck transaction in a new child transaction with a high fee. Miners evaluate the parent and child as a package, so the package rate is the combined fee divided by the combined vsize:
def cpfp_child_fee(parent_vsize, parent_fee, child_vsize, target_rate):
needed = (parent_vsize + child_vsize) * target_rate
return max(0, needed - parent_fee)
# Parent: 141 vB paying 1 sat/vB. Child: 141 vB. Target package rate: 20 sat/vB.
print(cpfp_child_fee(141, 141, 141, 20)) # 5499 satsThe child pays 5,499 sats, which is about 39 sat/vB on its own. It has to overpay because it must also cover the parent's shortfall. Both methods cost extra, so setting an adequate rate at the start is cheaper than rescuing a transaction later.
Common Errors
- Estimating the fee as a percentage of the amount sent.
- Using raw bytes instead of vbytes with a sat/vB rate.
- Forgetting that a legacy or wrapped-SegWit input costs more than a native SegWit one.
- Setting the fee rate from a stale estimate during a mempool surge.
- Creating change outputs below the dust limit, which nodes reject, instead of adding the leftover to the fee.
Exercise
A wallet spends 2 native SegWit (P2WPKH) inputs and creates 2 P2WPKH outputs, paying 25 sat/vB. Using the size table, what is the vsize and the fee in sats? If the same payment used 1 input instead, by how much would the fee change?
Show answer
With 2 inputs, vsize = ceil(10.5 + 2 × 68 + 2 × 31) = ceil(208.5) = 209 vB, so the fee is 209 × 25 = 5,225 sats. With 1 input, vsize = ceil(140.5) = 141 vB, so the fee is 141 × 25 = 3,525 sats. The second input adds 68 vB, which costs 68 × 25 = 1,700 sats more, and that matches the difference of 5,225 − 3,525.
Exercise
A parent transaction of 200 vB paid 200 sats. You build a child of 150 vB. How much must the child pay for the package to reach 15 sat/vB?
Show answer
The package needs (200 + 150) × 15 = 5,250 sats in total. The parent already paid 200 sats, so the child must pay 5,250 − 200 = 5,050 sats.