AG Studio: Build dashboards using native components in web apps. Join us for a webinar on 28th July at 2pm UTC+1 Register

JavaScript Embedded AnalyticsLLM Adapter

Version 2.1.2

AG Studio is provider-agnostic - it does not bundle a connection to any LLM. The adapter is the seam between Studio and your provider. You implement the AgAiAssistant interface, which translates between Studio's request format and your chosen LLM.

The example below ships a complete OpenAI adapter. Copy it as a starting point and adapt it to your provider.

The AgAiAssistant Interface Copy Link

The adapter is a plain object. Its one required method is executeTurn.

executeTurnCopy Link
Function
Execute a single turn of conversation with the AI. A turn consists of sending input and receiving a streamed response.
agentsCopy Link
(AgAiAgent | AgAiBuiltInAgent)[]
The set of agents the active runtime runs — each agent is instructions + tools + a delegation graph. When omitted, the built-in runtime uses AG's default agents. Supply your own (compose with agStudioDefaultAgents to keep AG's) to fully control the set. This is orchestration policy interpreted by the active runtime: the built-in runtime runs these as its agents; a custom runtime may own its own agents and ignore this field.
primaryAgentCopy Link
string
Type of the agent the conversation starts from. Defaults to 'lead'. Interpreted by the active runtime alongside agents.

The agents and primaryAgent fields configure the agents the built-in runtime uses - see Custom Agents.

executeTurn Copy Link

executeTurn is called each time Studio needs an AI response. It receives an AgAiRequest and must return an AgAiResponseHandler synchronously. The handler exposes a live stream and a completion promise.

The Request Copy Link

Each call to executeTurn receives everything the model needs for one turn.

AgAiConversationItem[]
Conversation history to send to the AI, providing context.
instructionsCopy Link
string
System instructions for this specific turn, overriding defaults.
AgToolSchema[]
Tools available for the AI to use during this turn.
toolChoiceCopy Link
"none" | "required" | "auto" | AgAiToolChoice
Strategy for how the AI should choose tools.
  • 'auto': AI decides whether to use tools
  • 'none': AI must not use tools
  • 'required': AI must use at least one tool
  • AgAiToolChoice: AI must use the specified tool
  • responseFormatCopy Link
    AgAiJsonFormat | AgAiTextFormat
    Output format configuration controlling response structure.

    The Response Copy Link

    executeTurn returns an AgAiResponseHandler - a stream of incremental events and a complete promise that resolves with the final response.

    streamCopy Link
    { [asyncIterator]: any }
    Async iterable of stream events for real-time updates. Events are yielded as they arrive from the AI provider.
    completeCopy Link
    Promise<AgAiResponse>
    Promise that resolves when the response is fully complete. Contains the final, consolidated response data.

    Stream Events Copy Link

    The stream yields AgAiStreamEvent values. Each has a type and an event discriminator:

    TypeEventDescription
    statuscreatedThe provider has created the response object.
    statusin_progressThe model is actively generating.
    statuscompletedGeneration finished. Includes the final AgAiResponse.
    statusfailedGeneration failed.
    errorapi, network, timeout, etc.An error occurred. Includes code and message.
    itemaddedA new output item (message, tool call, reasoning) started.
    itemdoneAn output item finished.
    partaddedA content part within an item started.
    partdoneA content part finished.
    deltaupdateIncremental content to append.
    deltadoneFinal content for a part.

    Tool Calls Copy Link

    The adapter does not execute tools. It only:

    1. Passes the AgToolSchema[] in request.tools to the LLM.
    2. Relays the tool-call output items from the LLM back through the stream.

    The runtime intercepts those tool calls, executes them, and feeds the results back as function_call_output items on the next turn. Your adapter never needs to know what view_schema or configure_widget do.

    Keeping Keys Off the Client Copy Link

    executeTurn runs in the browser, so calling a provider directly exposes your API key. For production, point executeTurn at your own backend endpoint instead: forward the AgAiRequest, call the provider server-side with your secret key, and stream the response back. The adapter contract is unchanged - only the URL it calls differs.

    Interface Reference Copy Link

    string
    Unique identifier for this response.
    createdAtCopy Link
    number
    Timestamp when the response was created (milliseconds since epoch).
    AgAiResponseError
    Error details if the response failed.
    incompleteDetailsCopy Link
    AgAiResponseIncompleteDetails
    Details about why the response was incomplete. Present when the AI couldn't fully complete its response.
    outputCopy Link
    AgAiOutputItem[]
    Output items produced by the AI (messages, tool calls, reasoning).
    statusCopy Link
    "completed" | "failed" | "in_progress" | "cancelled" | "queued" | "incomplete"
    Current status of the response.
    string
    Tool name.
    descriptionCopy Link
    string
    Human-readable description for the LLM.
    JSON Schema describing the tool's parameters.

    Next Steps Copy Link