Embed PiG with the Go SDK

Start a PiG session from Go code, send a prompt, read the reply.

Guides / Embed PiG

Tested · 2026-11-06

Use the Go SDK when your program is written in Go and does not need a process boundary. It is the same agent that the pig command runs, as a library.

Prerequisites: Go 1.26 or later, and a configured model (pig login or provider keys) in the agent directory the SDK reads.

Step 1: add the module

go mod init example.com/demo
go get github.com/MichaelKinsy/PiG@v0.7.0

Step 2: write the program

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/MichaelKinsy/PiG/coding"
)

func must(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    services, err := coding.CreateAgentSessionServices(coding.CreateAgentSessionServicesOptions{CWD: ".", AgentDir: coding.DefaultAgentDir()})
    must(err)
    rt, err := coding.NewRuntime(coding.RuntimeOptions{Services: services})
    must(err)
    model, err := coding.BuildModel("fake/fake-model", services)
    must(err)
    session, err := rt.New(coding.SessionStartOptions{Model: model})
    must(err)
    _, err = session.Send(context.Background(), "Say hello")
    must(err)
    fmt.Println(*session.LastAssistantText())
}

Replace fake/fake-model with a model you have configured. The model string is provider/model; pig --list-models shows what your installation offers.

Step 3: build and run

go mod tidy
go vet ./...
go build -o sdkdemo .
./sdkdemo

Verify

Output of that sequence (pig 0.7.0 module, Linux amd64, a local scripted model, 2026-11-06):

$ go vet ./...
vet exit 0
$ go build -o sdkdemo .
build exit 0
$ ./sdkdemo
Hello! This reply comes from a local fake model.

The program printed the model's reply. The full API is in the Go SDK reference: sessions, services, tools, extensions, cancellation and the harness events.

Test record

Tested 2026-11-06 with pig 0.7.0 on Linux amd64, using a local scripted model so no key is needed. Where a step needs a real model or a published artifact that we did not use, the guide says so.