---
title: How to Calculate Transaction Cost
description:
  "Every transaction costs compute units and a fee in lamports to execute on
  Solana. Learn how to calculate the cost of a transaction on Solana."
---

Every transaction consumes compute units and requires a transaction fee in
lamports to execute. The number of signatures included on a transaction
determines the base transaction fee (5000 lamports per signature).

<CodeTabs storage="cookbook" flags="r">

```typescript !! title="Kit" file=packages/docs-examples/cookbook/transactions/calculate-cost/kit.ts#region=cost

```

```typescript !! title="Legacy" file=packages/docs-examples/cookbook/transactions/calculate-cost/legacy.ts#region=cost

```

```rust !! title="Rust" file=packages/docs-examples/cookbook/transactions/calculate-cost/rust/src/main.rs#region=cost

```

```py !! title="Python"
#!/usr/bin/env python3
"""
Solana Cookbook - How to Calculate Transaction Cost
"""

import asyncio
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
from solders.system_program import transfer, TransferParams
from solders.transaction import VersionedTransaction
from solders.message import MessageV0

async def main():
    rpc = AsyncClient("http://localhost:8899")

    sender = Keypair()
    recipient = Keypair()

    amount = 1_000_000_000  # 1 SOL

    async with rpc:
        # Get latest blockhash
        latest_blockhash = await rpc.get_latest_blockhash()

        # Create transfer instruction
        transfer_instruction = transfer(
            TransferParams(
                from_pubkey=sender.pubkey(),
                to_pubkey=recipient.pubkey(),
                lamports=amount
            )
        )

        # Create message
        message = MessageV0.try_compile(
            payer=sender.pubkey(),
            instructions=[transfer_instruction],
            address_lookup_table_accounts=[],
            recent_blockhash=latest_blockhash.value.blockhash
        )

        # Create transaction
        transaction = VersionedTransaction(message, [sender])

        # Get fee for transaction
        fee_response = await rpc.get_fee_for_message(message)

        print(f"Transaction fee: {fee_response.value} lamports")
        print(f"Transaction fee: {fee_response.value / 1_000_000_000} SOL")

        # Calculate total cost (amount + fee)
        total_cost = amount + fee_response.value
        print(f"Total cost: {total_cost} lamports")
        print(f"Total cost: {total_cost / 1_000_000_000} SOL")

if __name__ == "__main__":
    asyncio.run(main())
```

</CodeTabs>
