Go SDK

Updated

API reference for the Conversational AI Platform (Preview) features of the Agora agents Go SDK, including saved agents, sessions, secrets, numbers, and campaigns.

Preview documentation

You're reading the Conversational AI Platform (Preview) SDK docs. Switch to Main.

The github.com/AgoraIO/agora-agents-go/v2 Go SDK supports the Conversational AI Platform (Preview) alongside the Voice Agent API. This page covers the Platform features: saved agents, Platform sessions, secret sets, phone numbers, campaigns, and the low-level client.V3 client.

The vendor constructors, such as vendors.NewOpenAI() and vendors.NewDeepgramSTT(), and the builder methods, such as WithLlm(), are shared with the Voice Agent API. For those, see the Go SDK reference for Voice Agents.

Install

Platform support is in the SDK preview, version v2.14.0-alpha.3. You need Go 1.21 or later. Add the SDK to your Go module:

go get github.com/AgoraIO/agora-agents-go/v2@v2.14.0-alpha.3
go mod tidy

Import the packages you use:

import (
	"github.com/AgoraIO/agora-agents-go/v2/agentkit"
	"github.com/AgoraIO/agora-agents-go/v2/agentkit/vendors"
	"github.com/AgoraIO/agora-agents-go/v2/option"
	"github.com/AgoraIO/agora-agents-go/v2/v3"
)

The v3 package holds the low-level Platform types, such as v3.ListOptions. Import it when you use them.

AgoraClient

Create a client with one of the following authentication methods. To choose one, see Choose an authentication method.

  • Token authentication: Pass your App ID and App Certificate. The SDK generates a ConvoAI token for each Platform request.

    client := agentkit.NewAgoraClient(agentkit.AgoraClientOptions{
    	Area:           option.AreaUS,
    	AppID:          os.Getenv("AGORA_APP_ID"),
    	AppCertificate: os.Getenv("AGORA_APP_CERTIFICATE"),
    })
  • Basic HTTP authentication: Pass your App ID, customer ID, and customer secret. The SDK sends Authorization: Basic <base64_credentials> with each request.

    client := agentkit.NewAgoraClient(agentkit.AgoraClientOptions{
    	Area:           option.AreaUS,
    	AppID:          os.Getenv("AGORA_APP_ID"),
    	CustomerID:     os.Getenv("AGORA_CUSTOMER_ID"),
    	CustomerSecret: os.Getenv("AGORA_CUSTOMER_SECRET"),
    })
FieldTypeDescription
Areaoption.AreaRequired. The region for Voice Agent API routing. Platform requests use the Platform origin.
AppIDstringRequired. Your Agora App ID.
AppCertificatestringYour App Certificate. With token authentication, the SDK uses it to generate a token for each request. With Basic authentication, it's optional, and the SDK uses it only to generate the agent's RTC token.
TokenstringA token you generated yourself, instead of AppCertificate. The token must include the ConvoAI service.
CustomerIDstringYour customer ID, for Basic authentication. Set it with CustomerSecret. Don't combine it with Token.
CustomerSecretstringYour customer secret, for Basic authentication.
V3*agentkit.V3OptionsPlatform request settings. BaseURL overrides the Platform origin, which defaults to https://preview.ai.agora.io. You can also set the AGORA_AGENTS_V3_BASE_URL environment variable. The option takes precedence.

Without an App Certificate, the SDK can't generate the agent's RTC token. To start an RTC session with a Basic client and no App Certificate, pass the RTC token when you create the session.

The client exposes the following Platform resources:

FieldDescription
client.V3The low-level Platform client: Agents, Secrets, Numbers, Sessions, and Campaigns. See Low-level API.
client.SecretsThe same client as client.V3.Secrets.
client.NumbersThe same client as client.V3.Numbers.
client.CampaignsThe same client as client.V3.Campaigns.

Agent

*agentkit.Agent is an immutable builder. Each builder method returns a new *Agent. To save a configuration on the Platform, build it with the vendor constructors and call Save().

agent := agentkit.NewAgent(client).
	WithStt(vendors.NewDeepgramSTT(vendors.DeepgramSTTOptions{Model: "nova-3", Language: "en-US"})).
	WithLlm(vendors.NewOpenAI(vendors.OpenAIOptions{Model: "gpt-4.1-mini"})).
	WithTts(vendors.NewOpenAITTS(vendors.OpenAITTSOptions{Model: "tts-1", Voice: "alloy"})).
	WithVariable("customer_name", agentkit.VariableDefinition{Type: "str", Required: true})

savedAgent, err := agent.Save(ctx, agentkit.SaveOptions{Name: "Support agent"})

The vendor constructors and WithVariable() panic on invalid input, because they don't return an error.

Variables

func (a *Agent) WithVariable(name string, def VariableDefinition) *Agent

Declares a variable and returns a new *Agent. def is an agentkit.VariableDefinition:

FieldTypeDescription
TypestringRequired. The data type: "str", "int", "float", "bool", "date", or "datetime".
RequiredboolWhether every session must pass a value.
Options[]anyThe allowed values, 1 to 100 unique items.
ExtractboolWhether to extract the value after the session ends. Requires Description.
DescriptionstringUp to 2,000 characters. Required when Extract is true.

A variable name starts with a lowercase letter and contains up to 64 lowercase letters, digits, and underscores.

Save an agent

func (a *Agent) Save(ctx context.Context, opts SaveOptions) (*SavedAgent, error)

Saves the configuration and returns a *SavedAgent. opts is an agentkit.SaveOptions with Name and AgentID. Without AgentID, it creates a new agent. Names aren't unique, so saving twice creates two agents. To update a saved agent, prefer savedAgent.Update().

Start a session without saving

func (a *Agent) CreateSession(opts CreateSessionOptions) *AgentSession

Creates a session from the builder without saving it. The SDK starts an ephemeral session on the Platform when the configuration has a secret reference or a variable, or when you set a Platform session option such as MaxDuration, Values, Labels, or DataPolicy. Otherwise, it uses the Voice Agent API. For the options, see Session options.

How the builder maps to the Platform

  • Credentials: A model without an API key uses managed mode. A model with an API key uses BYOK. A saved agent must use an agentkit.SecretRef() as the key. An ephemeral session also accepts a key value.
  • Instructions and messages: The LLM's system messages become the agent's instructions. Its other messages become messages. GreetingMessage and GreetingConfigs become the agent's greeting.
  • Pipeline: Only cascaded ASR, LLM, and TTS pipelines are supported. An MLLM configuration can't be saved or used in a Platform session.
  • Unsupported fields: Builder settings without a Platform equivalent, such as SAL, advanced features, labels, RTC settings, and pipeline IDs, make Save() return an error that wraps agentkit.ErrUnsupportedField and lists them.
  • Generated types: Interruption, turn detection, and filler word settings use generated types from the root package. Where agentkit has a shorter alias, such as agentkit.FillerWordsContent or agentkit.InterruptionModeKeywords, use it.

Secret references

func SecretRef(set, key string) string

Returns the reference $secrets.<set>.<key> for a key in a secret set. It validates both names locally, makes no request, and panics on an invalid name. Each name contains 1 to 64 letters, digits, and underscores.

SavedAgent

A handle to an agent saved on the Platform.

MemberDescription
IDThe agent ID.
NameThe agent name.
ETagThe agent's ETag when the handle was loaded or saved.
RawThe client.V3.Agents client.
agentkit.GetSavedAgent(ctx, client, id)Loads a saved agent. Returns (*SavedAgent, error).
CreateSession(opts)Creates a session that runs this agent. Returns an *AgentSession. See Session options.
Update(ctx, config)Saves a revised *Agent to this agent ID, and keeps its name. The *Agent must use the same client. Returns (*SavedAgent, error).
Delete(ctx)Deletes the agent.
AssignNumber(ctx, number, opts)Routes inbound calls on a phone number to this agent. number is a number ID, an E.164 string, or a v3.Number. opts is an agentkit.AssignNumberOptions with MaxCallDurationSeconds and MaxSilenceDurationMs (both *int). Returns (*v3.Number, error).
UnassignNumber(ctx, number)Removes this agent from a number's inbound routing.
ListNumbers(ctx)Returns the numbers assigned to this agent. Scans all project numbers.

A handle is a snapshot. If another process changes the agent, load it again with agentkit.GetSavedAgent().

Session options

savedAgent.CreateSession() and agent.CreateSession() take an agentkit.CreateSessionOptions. The following fields apply to Platform sessions:

FieldTypeDescription
ChannelstringRequired. The RTC channel.
TokenstringThe RTC token for the agent's UID. The SDK doesn't generate it for Platform sessions.
AgentUIDstringThe agent's UID in the channel. Omit it to have the Platform assign one.
RemoteUIDs[]stringRequired. The UID of the user the agent talks to. Pass exactly one.
Valuesmap[string]anyValues for the agent's variables.
Labelsmap[string]stringLabels returned in notification events.
IdleTimeout*intSeconds without a user before the session ends, 0 to 3600.
MaxDuration*intThe maximum session length in seconds, 1 to 3600.
GracefulStop*agentkit.GracefulStopOptionsLets the agent finish speaking before it stops. TimeoutMS is 0 to 120000.
DataPolicy*agentkit.SessionDataPolicyWhether Agora retains the session's conversation data. Set Retention to "30_days" or "none".
Encryption*agentkit.RTCEncryptionRTC media encryption settings.
AudioScenariostringThe RTC audio scenario, such as "aiserver".
Geofence*agentkit.SessionGeofenceRestricts the regions the session connects through.
IdempotencyKeystringAn Idempotency-Key for the start request. Retry an uncertain start with the same key: if the first attempt started the session, the retry fails with 409 IdempotencyKeyConflict instead of starting another. Use a new key for a new session.

AgentSession

A session handle. On the Platform, the following methods are available:

MethodDescription
Start(ctx)Starts the session. Returns (string, error), the session ID.
GetInfo(ctx)Retrieves the session. The result includes Status.
Update(ctx, properties)Updates a running session. Accepts only the RTC token, the conversation messages, and the LLM params. A params object replaces the current one.
Stop(ctx)Stops the session.

Say(), Interrupt(), and the history and turns methods return agentkit.ErrUnsupportedSessionOperation on Platform sessions.

To read or stop a session by ID without a handle, use client.V3.Sessions.Get(ctx, id) and client.V3.Sessions.Stop(ctx, id).

Low-level API

client.V3 has one client for each part of the Platform REST API. Every method takes ctx first and returns (*v3.Response[T], error). A v3.Response has Body (the parsed response), Header, ETag, RequestID, and StatusCode.

Pass "" for an optional string argument, such as an ifMatch or an idempotency key, to leave it out.

current, err := client.V3.Agents.Get(ctx, agentID)
if err != nil {
	log.Fatal(err)
}
etag := current.ETag

Agents

MethodREST API
Agents.Create(ctx, body)Create an agent
Agents.List(ctx, v3.ListOptions)List agents
Agents.Get(ctx, id)Retrieve an agent
Agents.Update(ctx, id, ifMatch, patch)Update an agent
Agents.Delete(ctx, id)Delete an agent

Sessions

MethodREST API
Sessions.Start(ctx, body, opts...)Start a session
Sessions.StartEphemeral(ctx, body, opts...)Start an ephemeral session
Sessions.List(ctx, v3.SessionListOptions)List sessions
Sessions.Get(ctx, id)Retrieve a session
Sessions.Update(ctx, id, patch)Update a session
Sessions.Stop(ctx, id)Stop a session

Secrets

MethodREST API
Secrets.Create(ctx, body, "")Create a secret set
Secrets.List(ctx, v3.ListOptions)List secret sets
Secrets.Get(ctx, name)Retrieve a secret set
Secrets.Update(ctx, name, ifMatch, body)Update a secret set
Secrets.Replace(ctx, name, ifMatch, body)Replace a secret set
Secrets.Delete(ctx, name, ifMatch)Delete a secret set
Secrets.SetKey(ctx, name, key, v3.SecretKeyWrite, v3.SecretKeyOptions)Set a secret key
Secrets.DeleteKey(ctx, name, key, ifMatch)Delete a secret key

Secrets.Create and Numbers.Create take an idempotency-key argument for compatibility. Pass "". A non-empty key returns an error.

Phone numbers

MethodREST API
Numbers.Create(ctx, body, "")Create a phone number
Numbers.List(ctx, v3.ListOptions)List phone numbers
Numbers.Get(ctx, id)Retrieve a phone number
Numbers.Update(ctx, id, ifMatch, patch)Update a phone number
Numbers.Delete(ctx, id)Delete a phone number

Campaigns

MethodREST API
Campaigns.Create(ctx, body, opts...)Create a campaign
Campaigns.List(ctx, v3.CampaignListOptions)List campaigns
Campaigns.Get(ctx, id)Retrieve a campaign
Campaigns.Update(ctx, id, ifMatch, body)Update a campaign
Campaigns.Delete(ctx, id)Delete a campaign
Campaigns.Pause(ctx, id, opts...)Pause a campaign
Campaigns.Resume(ctx, id, opts...)Resume a campaign
Campaigns.Cancel(ctx, id, opts...)Cancel a campaign

Pagination

Each List method returns one page. The page body has Data.Count, Data.List, HasMore, and NextCursor. To read every page, pass NextCursor as Cursor until HasMore is false:

cursor := ""
for {
	page, err := client.V3.Agents.List(ctx, v3.ListOptions{Limit: 100, Cursor: cursor})
	if err != nil {
		log.Fatal(err)
	}
	for _, item := range page.Body.Data.List {
		fmt.Println(item.AgentID, item.AgentName)
	}
	if !page.Body.HasMore || page.Body.NextCursor == nil || *page.Body.NextCursor == "" {
		break
	}
	cursor = *page.Body.NextCursor
}

v3.SessionListOptions adds Channel, Status, StartedAfter, and StartedBefore. Without Status, Sessions.List returns running sessions. To list others, pass a comma-separated list, for example "STOPPED,FAILED".

Conditional writes and retries

Updates to agents, numbers, and campaigns, and deletes and replacements of secret sets, require the resource's current ETag in ifMatch. Get it from Response.ETag. If the resource changed since you read it, the method returns a *v3.PreconditionFailedError. Call its Latest(ctx) method to reload the resource and its new ETag. It doesn't retry the write.

The SDK makes one attempt for every write. To retry a session start safely, pass the same idempotency key. Other operations don't support idempotency keys.

Errors

ErrorReturned when
*core.APIErrorThe Platform returns an error. It has StatusCode and the response body, which usually has reason and detail. Import github.com/AgoraIO/agora-agents-go/v2/core, and match it with errors.As. For the reasons, see Status codes.
*v3.PreconditionFailedErrorA conditional write fails with 412. Match it with errors.As.
agentkit.ErrPreconditionFailedsavedAgent.Update() fails with 412. Match it with errors.Is.
agentkit.ErrUnsupportedFieldThe builder has settings with no Platform equivalent. Returned before any request.
agentkit.ErrSecretRefNotSupportedA secret reference is used on the Voice Agent API.
agentkit.ErrNumberNotFoundAssignNumber() or UnassignNumber() can't find the number in the project.
agentkit.ErrUnsupportedSessionOperationA session method isn't available on Platform sessions.