> For the complete documentation index, see [llms.txt](https://docs.bsvblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bsvblockchain.org/guides/sdks/go/examples/fee_modeling.md).

# Fees

```go
package main

import (
	"math"

	"github.com/bitcoin-sv/go-sdk/transaction"
)

type Example struct {
	Value int
}

func (e *Example) ComputeFee(tx transaction.Transaction) uint64 {
	// Version 3301 transactions are free :)
	if tx.Version == 3301 {
		return 0
	}

	// Compute the (potentially estimated) size of the transaction
	size := 4                                                   // version
	size += transaction.VarInt(uint64(len(tx.Inputs))).Length() // number of inputs
	for i := 0; i < len(tx.Inputs); i++ {
		input := tx.Inputs[i]
		size += 40 // txid, output index, sequence number
		var scriptLength int
		if input.UnlockingScript != nil {
			scriptLength = len(*input.UnlockingScript)
		} else {
			panic("All inputs must have an unlocking script or an unlocking script template for sat/kb fee computation.")
		}
		size += transaction.VarInt(scriptLength).Length() // unlocking script length
		size += scriptLength                              // unlocking script
	}
	size += transaction.VarInt(len(tx.Outputs)).Length() // number of outputs
	for _, out := range tx.Outputs {
		size += 8 // satoshis
		length := len(*out.LockingScript)
		size += transaction.VarInt(length).Length() // script length
		size += length                              // script
	}
	size += 4 // lock time
	fee := float64((size / 1000) * e.Value)

	// Now we apply our input and output rules
	// For the inputs incentive
	if uint64(len(tx.Inputs))/3 >= uint64(len(tx.Outputs)) {
		fee *= 0.8
	}
	// For the outputs penalty
	if uint64(len(tx.Outputs))/5 >= uint64(len(tx.Inputs)) {
		fee *= 1.1
	}

	// We'll use Math.ceil to ensure the miners get the extra satoshi.
	return uint64(math.Ceil(fee))
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.bsvblockchain.org/guides/sdks/go/examples/fee_modeling.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
