Create a Open AI Agent with Go Lang

Introduction

If you’re into AI and love working with Go, you’ll want to check out agent-sdk-go on GitHub. This SDK makes it easy to build AI agents that can integrate with different LLMs, use tools, and even perform agent-to-agent handoffs.

In this guide, we’ll walk through how to:

  • Install agent-sdk-go
  • Set up an LLM provider (LM Studio)
  • Create an agent
  • Add tools to the agent
  • Run the agent and get responses

Let’s dive in! :rocket:


Step 1: Installation

First, install the SDK by running:

go get github.com/pontus-devoteam/agent-sdk-go

Alternatively, add it to your imports and run:

go mod tidy

Make sure you’re using Go 1.23 or later.


Step 2: Setting Up an LLM Provider

We need an LLM provider to process our prompts. This example uses LM Studio as the provider.

Start LM Studio

Ensure LM Studio is running locally and exposing an API (e.g., http://127.0.0.1:1234/v1).

Now, set up the provider in Go:

provider := lmstudio.NewProvider()
provider.SetBaseURL("http://127.0.0.1:1234/v1")
provider.SetDefaultModel("gemma-3-4b-it") // Replace with your model

Step 3: Creating an Agent

Now, let’s create an agent that interacts with the model.

assistant := agent.NewAgent("Assistant")
assistant.SetModelProvider(provider)
assistant.WithModel("gemma-3-4b-it")
assistant.SetSystemInstructions("You are a helpful assistant.")

This agent will communicate with the model and respond based on system instructions.


Step 4: Adding Tools to the Agent

Agents can call external functions using tools. Let’s add a tool that provides weather updates:

tool := tool.NewFunctionTool(
    "get_weather",
    "Get the weather for a city",
    func(ctx context.Context, params map[string]interface{}) (interface{}, error) {
        city := params["city"].(string)
        return fmt.Sprintf("The weather in %s is sunny.", city), nil
    },
).WithSchema(map[string]interface{}{
    "type": "object",
    "properties": map[string]interface{}{
        "city": map[string]interface{}{
            "type": "string",
            "description": "The city to get weather for",
        },
    },
    "required": []string{"city"},
})

Attach this tool to the agent:

assistant.WithTools(tool)

Step 5: Running the Agent

Use a Runner to execute the agent and get a response.

runner := runner.NewRunner()
runner.WithDefaultProvider(provider)

result, err := runner.RunSync(assistant, &runner.RunOptions{
    Input: "What's the weather in Tokyo?",
})
if err != nil {
    log.Fatalf("Error running agent: %v", err)
}

fmt.Println(result.FinalOutput)

This will send the input to the agent, which calls the get_weather tool and returns a response.


Conclusion

With agent-sdk-go, you can easily build powerful AI agents in Go. You can:
:white_check_mark: Use multiple LLM providers
:white_check_mark: Add tools for custom function calls
:white_check_mark: Create structured outputs
:white_check_mark: Build multi-agent workflows

Give it a try and let me know what you build! :fire:

1 Like