Bitcoin Transaction Fees
Complete guide to Bitcoin fees for agents. Fee estimation, RBF, CPFP, and optimization strategies.
Bitcoin Transaction Fees
Transaction fees incentivize miners to include transactions in blocks. Understanding fee dynamics is critical for agents to avoid overpaying or having transactions stuck.
How Fees Work
Fee = Total Input Value - Total Output Value
Fees are implicit—whatever isn’t assigned to outputs goes to miners.
Fee Rate
Fees are priced per virtual byte (vB):
fee_rate = total_fee / transaction_vsize (sat/vB)
Miners prioritize transactions with higher sat/vB rates.
Fee Estimation
API Endpoints
# mempool.space recommended fees
curl https://mempool.space/api/v1/fees/recommended
Response:
{
"fastestFee": 45,
"halfHourFee": 35,
"hourFee": 25,
"economyFee": 15,
"minimumFee": 8
}
Fee Tiers
| Tier | Target | Typical Use |
|---|---|---|
fastestFee | Next block | Urgent payments |
halfHourFee | ~3 blocks | Standard payments |
hourFee | ~6 blocks | Non-urgent |
economyFee | 1+ hours | Batch consolidation |
minimumFee | Eventually | Low priority |
Historical Fee Data
# Fee histogram (current mempool)
curl https://mempool.space/api/mempool
# Block fee stats
curl https://mempool.space/api/v1/blocks/tip/fees
Transaction Size Factors
Virtual Size by Input Type
| Input Type | vsize per input |
|---|---|
| P2PKH | ~148 vB |
| P2SH-P2WPKH | ~91 vB |
| P2WPKH | ~68 vB |
| P2TR | ~57.5 vB |
Base Transaction Overhead
| Component | Size |
|---|---|
| Version | 4 bytes |
| Locktime | 4 bytes |
| Input count | 1-3 bytes |
| Output count | 1-3 bytes |
| Per output (P2WPKH) | 31 bytes |
Size Calculation Example
P2WPKH → P2WPKH (1 input, 2 outputs):
- Overhead: ~10.5 vB
- Input: 68 vB
- Outputs: 31 × 2 = 62 vB
- Total: ~141 vB
At 20 sat/vB: 141 × 20 = 2,820 sats (~$2.82 at $100k/BTC)
Fee Optimization Strategies
1. Use Efficient Address Types
| Address Type | Savings vs P2PKH |
|---|---|
| P2SH-P2WPKH | ~38% |
| P2WPKH | ~54% |
| P2TR | ~61% |
Always use SegWit or Taproot addresses.
2. Batch Transactions
Instead of N separate transactions, batch into one:
# Separate: 3 transactions at 141 vB each = 423 vB
# Batched: 1 transaction (1 input, 4 outputs) = ~172 vB
# Savings: 59%
3. Consolidate UTXOs
During low-fee periods, combine small UTXOs:
# Find low-fee windows
if current_fee < historical_average * 0.5:
consolidate_utxos()
4. Time Transactions
Fee markets fluctuate:
- Lowest: Weekends, late nights (UTC)
- Highest: Weekday mornings, market volatility
Replace-By-Fee (RBF)
RBF allows replacing unconfirmed transactions with higher-fee versions.
Enabling RBF
Set input sequence < 0xfffffffe:
{
"vin": [{
"sequence": 4294967293 // 0xfffffffd
}]
}
RBF Rules (BIP-125)
- Original must signal replaceability
- Replacement must pay absolutely higher fee
- Replacement must have higher fee rate
- Must pay for bandwidth of both transactions
- Max 100 unconfirmed descendants
Bumping Fee via API
# Create replacement transaction
# Same inputs, higher fee, optionally same outputs
curl -X POST https://mempool.space/api/tx \
-d "0200000001..." # replacement tx hex
Child-Pays-For-Parent (CPFP)
Accelerate stuck transactions by spending their outputs with high fees.
How CPFP Works
Parent TX (stuck at 5 sat/vB, 200 vB)
↓
Child TX (spends parent output, pays 100 sat/vB, 150 vB)
↓
Combined rate: (5×200 + 100×150) / (200+150) = 45.7 sat/vB
When to Use CPFP
| Scenario | Use RBF | Use CPFP |
|---|---|---|
| You sent the TX | ✓ (preferred) | ✓ |
| You received the TX | ✗ | ✓ |
| TX has RBF disabled | ✗ | ✓ |
Minimum Relay Fee
Transactions below minimum fee won’t propagate:
Default minimum: 1 sat/vB
Dynamic minimum: Based on mempool congestion
Check current minimum:
curl https://mempool.space/api/mempool | jq '.mempoolminfee'
Dust Threshold
Outputs below dust threshold are non-standard:
| Output Type | Dust Threshold |
|---|---|
| P2PKH | 546 sats |
| P2SH | 540 sats |
| P2WPKH | 294 sats |
| P2TR | 330 sats |
Never create outputs below dust threshold.
Agent Fee Strategy
Decision Tree
1. Is transaction urgent?
Yes → Use fastestFee + 10%
No → Continue
2. Is RBF acceptable?
Yes → Use hourFee, enable RBF
No → Use halfHourFee
3. Can you batch?
Yes → Batch multiple payments
No → Single transaction
4. Monitor mempool:
If stuck > 1 hour → Bump via RBF
If receiving → Consider CPFP
Code Example
def calculate_fee(vsize, urgency='normal'):
fees = requests.get('https://mempool.space/api/v1/fees/recommended').json()
rate_map = {
'urgent': fees['fastestFee'],
'normal': fees['halfHourFee'],
'economy': fees['economyFee']
}
rate = rate_map.get(urgency, fees['halfHourFee'])
fee = vsize * rate
# Add buffer for fee fluctuation
return int(fee * 1.1)
Fee Market Monitoring
Real-Time Mempool Stats
# Mempool size and fees
curl https://mempool.space/api/mempool
# Projected blocks
curl https://mempool.space/api/v1/fees/mempool-blocks
Response Example
{
"count": 45000,
"vsize": 85000000,
"total_fee": 12500000000,
"fee_histogram": [[25, 1500000], [20, 3000000], ...]
}
Machine-Readable Summary
{
"topic": "bitcoin-fees",
"fee_unit": "sat/vB",
"estimation_api": "https://mempool.space/api/v1/fees/recommended",
"optimization_strategies": ["segwit", "batching", "consolidation", "timing"],
"acceleration_methods": ["rbf", "cpfp"],
"dust_thresholds": {
"p2pkh": 546,
"p2wpkh": 294,
"p2tr": 330
}
}