agentscript

agentscript

Build AI agents that think in code. A Typescript framework for building reliable AI agents that express their plans as code and execute it in our custom built AST runtime: enabling stop/start workflows to keep humans in loop, tool-level state management, and enhanced observability.

Stars: 123

Visit
 screenshot

AgentScript is an open-source framework for building AI agents that think in code. It prompts a language model to generate JavaScript code, which is then executed in a dedicated runtime with resumability, state persistence, and interactivity. The framework allows for abstract task execution without needing to know all the data beforehand, making it flexible and efficient. AgentScript supports tools, deterministic functions, and LLM-enabled functions, enabling dynamic data processing and decision-making. It also provides state management and human-in-the-loop capabilities, allowing for pausing, serialization, and resumption of execution.

README:

AgentScript: Build AI agents that think in code

CodeCov  Website
Docs  Join our Discord community

AgentScript is a unique open-source framework for building re-act AI agents.

npm install agentscript-ai

Check out a short demo video:

AgentScript demo video

See also our examples repo.

What is it?

Typical Re-act agent work like this:

  1. Send to LLM a prompt with available tools
  2. LLM sends back a tool call
  3. Execute the tool and send back the result
  4. LLM responds with another tool call
  5. ...

Wouldn't be better if LLM just told you upfront what tools to use, in which order and what to do with the results?
LLMs are great at code generation, so maybe it could just write a code to express its plan!

AgentScript does exactly that: prompts an LLM to generate code (a subset of JS) and executes it in a dedicated runtime with resumability, state persistence and interactivity (human in the loop) baked in.

How it works?

  1. Define a runtime with tools and optionally input and output variables
  2. Define a task to be executed (aka prompt)
  3. AgentScript prompts LLM to genarate JS code
  4. Code it not executed directly but parsed into an AST
  5. AST is executed in a dedicated, safe runtime (interpreter).
    No sandbox is needed - the generated code is not running directly in Node.
  6. Execution can be paused, serialized into a database and resumed later.
    (like when a tool call requires human interaction or approval).

Show me the code

Let's build an agent doing stuff with Linear.
You can see full example here.

// Configure the language model
const llm = AnthropicModel({
    model: 'claude-3-5-sonnet-latest',
    apiKey: process.env.ANTHROPIC_API_KEY,
});

// Configure the Linear client
const linear = LinearClient({
    apiKey: process.env.LINEAR_API_KEY,
});

// Define available tools.
const tools = {
    // Needed for date calculation
    addToDate,
    // Turns data into text
    summarizeData: summarizeData({ llm }),
    // The real deal
    linear: {
        searchIssues: searchIssues({ llm, linear }),
    },
};

// Define a task for the agent
const prompt = 'Give me a progress update of tasks created in the last week';

// Define the expected output
const output = s.string();

// Let the LLM generate the AgentScript code
const agent = await inferAgent({
    tools,
    output,
    llm,
    prompt,
});

LLM responds with a plan:

I'll help create a progress update for recent tasks. Here's the plan:

  1. First, we'll search for all issues created in the last week
  2. Then we'll take those issues and generate a summary that includes:
    • How many tasks were created
    • Their current status distribution
    • Key highlights or patterns
  3. Format it in a clear, concise way

...and generates AgentScript code:

// NOTE: this is real code generated by LLM.
// it's not executed but parsed into AST and runs in a specialized runtime.

// Calculate the date from 7 days ago
const lastWeek = addToDate(new Date(), { days: -7 });

// Search for all issues created in the last week
const issues = linear.searchIssues({
    createdAfter: lastWeek,
    orderBy: 'createdAt',
});

// Create a summary of the issues found
result = summarizeData({
    data: issues,
    prompt: 'Create a progress update focusing on number of tasks created, their current status distribution, and any notable patterns. Format as a clear business update.',
});

Now execute the agent:

await executeAgent({ agent });

// See the output
console.log(agent.output);

// Check variables in the execution state
console.log(agent.root.variables);

How is it different from other frameworks?

Many products define agent as a fixed workflow (for example Glide).
They work very nice for well defined tasks, but fall short when the task is ambiguous or not known beforehand.

Then we have a bunch of orchestration frameworks (LangGraph, CrewAI, Inferable among others).
They provide architecture and make it easier to build classic re-act agents, where each tool call or decision point requires another LLM query. But this makes the LLM context grow quickly, is costly, slow and not flexible enough (try to implement a loop this way).

AgentScript takes a completely different approach. By making LLM express execution plan as code, agent can think more abstractly about the task and does not even need to know all the data to perform operations on it or make decisions. Just like a developer writing an app does not need to know all the data it would use - they can write code working on dynamic data by using if statements and loops.

Data is expressed as local variables and can be passed to tools, which can be normal deterministic functions, or LLM enabled ones, built using LangChain or any other library.

What about state management and human in the loop?

Because AgentScript works on AST, not really running the generated code, execution can be paused on each statement or a tool call. It can be serialized and put into a database, then retrieved and resumed from where it stopped.

Each tool would be able to store its state, wait for a user interaction, an event, or some time to pass. They will have built in interactivity and approval mechanism, so it will be easy to add human in the loop.

State of development

Right now we have a working code generation and runtime supporting tools and most common JS statements.

Feel free to play with it by forking our examples repo.

How to Join the Community

AgentScript is an open-source project, and we welcome contributions from everyone.

Current roadmap

  • Execution serialization and deserialization
  • More JS features:
    • if statements,
    • for loops,
    • template literals
    • arrow functions
    • unary and binary operators
  • Input variables
  • Tool state
  • Tool interactivity
  • Observability and debugging

For Tasks:

Click tags to check more tools for each tasks

For Jobs:

Alternative AI tools for agentscript

Similar Open Source Tools

For similar tasks

For similar jobs