dexcost
Go

Quickstart

Initialize the Go SDK and capture your first attributed LLM cost in a few lines of code.

Initialize dexcost, start a task, record a call, and end the task — that is all it takes to land a fully attributed cost record.

Initialize

Call dexcost.Init once at application startup with a Config struct, then defer dexcost.Close to flush and release resources on shutdown:

package main

import (
    "context"
    "fmt"
    "log"

    dexcost "github.com/DexwoxBusiness/dexcost-go"
)

func main() {
    if err := dexcost.Init(dexcost.Config{
        APIKey: "dx_live_your_key_here", // or set DEXCOST_API_KEY env var
    }); err != nil {
        log.Fatal(err)
    }
    defer dexcost.Close()

    // ... rest of your application
    _ = context.Background()
}

Init is safe to call multiple times — only the first call takes effect. Close stops the background pusher and flushes any remaining buffered events to the Control Layer.

Your first tracked call

Start a task with dexcost.StartTask, wrap your LLM client (or call RecordLLMCall directly), then end the task:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/shopspring/decimal"

    dexcost "github.com/DexwoxBusiness/dexcost-go"
)

func main() {
    if err := dexcost.Init(dexcost.Config{
        Storage: "local",
    }); err != nil {
        log.Fatal(err)
    }
    defer dexcost.Close()

    ctx := context.Background()

    // Start a task with customer and project attribution.
    ctx, task := dexcost.StartTask(ctx, "resolve_ticket",
        dexcost.WithCustomer("acme-corp"),
        dexcost.WithProject("support-q1"),
    )

    // Record an LLM call — cost is auto-computed from bundled pricing data.
    if err := task.RecordLLMCall("openai", "gpt-4o", 1000, 500); err != nil {
        log.Fatal(err)
    }

    // Record a non-LLM service cost with exact-precision decimal.
    if err := task.RecordCost("stripe_api", decimal.RequireFromString("0.025"),
        dexcost.WithOperation("payment_lookup"),
    ); err != nil {
        log.Fatal(err)
    }

    // End the task — aggregates LLM + external costs into TotalCostUSD.
    if err := task.End(dexcost.StatusSuccess); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Total cost: %s USD\n", task.Task.TotalCostUSD.String())
    _ = ctx // ctx carries the task for nested operations
}

StartTask returns a derived context.Context with the task attached. Any wrapper client or nested StartTask call that receives this context picks up the parent attribution automatically. task.End aggregates all event costs into task.Task.TotalCostUSD, LLMCostUSD, and ExternalCostUSD.

Run it locally

Pass Environment: "development" to dexcost.Init to enable dev mode. In dev mode, every recorded event is printed to stderr with a cost summary and no data is sent to the cloud — useful for local development and debugging:

if err := dexcost.Init(dexcost.Config{
    Environment: "development",
}); err != nil {
    log.Fatal(err)
}
defer dexcost.Close()

You can also set the DEXCOST_ENV=development environment variable instead of passing the field to Init. To run without any cloud key and store events only in the local SQLite buffer, pass Storage: "local" (no APIKey required):

dexcost.Init(dexcost.Config{Storage: "local"})

Next steps

  • Instrumentation — wrapper client usage for each LLM provider, explicit recording with RecordLLMCall, and HTTP cost tracking.
  • Configuration — all Config fields, environment variables, buffer location, flush tuning, and privacy options.

On this page