Skip to main content
This guide walks you through building a complete AI agent that can execute LLM-generated code safely. By the end, you’ll have a working agent that can process natural language requests and execute tool-based workflows.

What You’ll Build

An AI agent that:
  • Accepts natural language requests
  • Generates JavaScript orchestration code via LLM
  • Executes the code safely in Enclave
  • Returns structured results

Prerequisites

  • Node.js 18+
  • An OpenAI API key (or other LLM provider)
  • Basic TypeScript knowledge

Step 1: Project Setup

Step 2: Define Your Tools

Tools are the capabilities your agent exposes. Start with a simple set:

Step 3: Create the LLM Prompt

Build a prompt that instructs the LLM to generate AgentScript-compatible code:

Step 4: Set Up the Enclave

Configure the secure execution environment:

Step 5: Build the Agent

Combine LLM code generation with Enclave execution:

Step 6: Test Your Agent

Run it:
Expected output:

Step 7: Add Error Handling

Make your agent more robust:

Step 8: Add Streaming (Optional)

For real-time output, use EnclaveJS:

Best Practices

  1. Validate tools - Check tool arguments before execution
  2. Log everything - Tool calls, durations, results for debugging
  3. Set limits - Always configure timeout, maxToolCalls, maxIterations
  4. Handle retries - LLM-generated code may need refinement
  5. Sandbox per request - Create a new Enclave for each execution

Next Steps