githubEdit

Tutorial: Hot-Swappable AI Agents

Stof as portable & flexible agent behaviors

  • Store agent behaviors in your database as Stof artifacts

  • Update agent logic without redeploying

  • A/B test different agent strategies by swapping workflows

  • Roll back bad changes instantly

Intro

Executable data opens many doors. AI is one of the most obvious examples because there are numerous challenges that current languages and frameworks were not designed to address.

When it comes to AI agents, Stof can help as:

  • Portable and self-describing agent behavior

  • Dynamic contexts & model selection

  • Prompt development, management, and composition

  • Tools as data, with control, portability, and flexibility

Setup & Test

We'll use TypeScript with Stof for this example, showing a customer support AI agent where you can update the agent's behavior in real-time by swapping Stof workflow artifacts stored in your database.

Anthropic Stof API

Here is a very simple Stof <-> Anthropic API. We'll be using Stof as the complete interface for AI.

This will be included in our TypeScript example as a single string variable called "ANTHROPIC_STOF_API", but will include the value ".. See Setup .." as a placeholder to keep our examples neat and readable.

Create Stof Document

We'll create a Stof document in TypeScript with the following function:

Agent & Handler

We'll use the following two functions as a simulated handler and Stof agent interaction. In the real world, this would be more fleshed out, involving a Stof pipeline (typically using Run and other Stof features).

circle-info

For getAgent, you'd most likely store a Stof API in your DB and supply a function that uses that API. The entire agent API would then be hot-swappable.

Completed Template & Test

Here is the full TypeScript template we will use in its entirety:

Output

If you input your Anthropic API key and run deno run --allow-all example.ts, you'll see something like:

Hot Swap With Flag

We'll focus now just on the getAgent function, where our hot swap example resides. In real life, you have nearly unlimited options for what you could do here:

  • Access a DB

  • Call an API

  • Use feature flags (like our example)

  • Hardcode your agent

  • A dynamic or user-provided context & prompts

  • ... sky's the limit really

Updated GetAgent + Flag

Execution

Let's test this out with a quick user message of "I'm having issues with my order" with the two agent modes.

Output

Further Improvements & Ideas

Stof provides a lot of flexibility in your implementation, including importing/managing customer records and data, validations, schemas, etc.

Try importing customer records from JSON/TS that fit a Stof type, then alter the agent's behavior, customizing it to the customer dynamically.

circle-info

Organize data within your Stof document by root object or by sub-objects, where you can be sure imports/APIs don't collide (or do so gracefully).

Takeaways

circle-info

In the real world, you can do a lot to make the Stof/TS boundary more bulletproof. This includes limiting the amount of programmatic Stof being generated from TS with a stable internal Stof API, validating inputs, a tested AI API with retries, etc.

This is a simple example to serve as a foundation for experimentation with Stof and AI. With Stof's portability and flexibility, you can:

  • Store agent behaviors in your database as Stof artifacts

  • Update agent logic without redeploying

  • A/B test different agent strategies by swapping workflows

  • Roll back bad changes instantly

In addition, Stof can greatly simplify your application logic, especially in cases where your systems are distributed.

Last updated