Skip to main content
The Netra SDK exposes a simulation client that lets you:
  • Run simulations - Execute multi-turn conversations against your AI agent
  • Define tasks - Create custom task implementations to wrap your agent
  • Use lifecycle hooks - Run setup/teardown scripts around scenarios (beforeAll, beforeEach, before, after, afterEach, afterAll)
  • Control concurrency - Manage parallel execution for throughput
This page shows how to use netra.simulation to run multi-turn simulations and test your AI agents programmatically.

Getting Started

The simulation client is available on the main Netra entry point after initialization.

runSimulation

Execute a multi-turn conversation simulation against a dataset. Your task function is called repeatedly for each turn until the conversation completes.

Parameters (SimulationOptions)

Response: SimulationResult


BaseTask

Create a custom task by extending the BaseTask abstract class. Your implementation wraps your AI agent and handles the conversation flow.

Implementation Requirements


Hooks (Pre/Post Scripts)

Multi-turn datasets often share state across scenarios. Rather than merging scenarios or forcing sequential execution, use hooks to set up and tear down state around scenario runs. Hooks run entirely on the SDK side — script code never leaves your environment. Lightweight metadata (function name and description) is sent to the backend so the Netra UI can show which hooks are configured. In the Netra dashboard (multi-turn Scenario Run DetailsConversation tab), configured hooks appear in collapsible Pre-script / Post-script panels with each hook’s name and description. Item run status badges include Prescript Failed and Postscript Failed.

Hook points

Context passing

setupContext is the merge of beforeAll + beforeEach + item before. It is passed to BaseTask.run, item after, and afterEach. If a before hook fails mid-way, teardown still receives the furthest successfully built setupContext (so afterEach can clean up what beforeEach created). afterAll receives only the run-level sharedContext, but its results include setup/first-turn failures as well as conversation failures. Prefer beforeEach / afterEach when the same setup/teardown applies to every scenario. Use item-keyed before / after for scenario-specific logic.

SimulationHooks

All hooks can be sync or async. TypeScript has no runtime docstrings — set a .description property on each hook function so the Netra UI can display it (truncated to 200 characters).

Example

Keys in before / after must be real datasetItemId values from your dataset. A prescript_failed item is terminal (eval suppressed); other scenarios continue. A postscript_failed item is also terminal, but eval is not suppressed — conversation evaluations remain valid. Wrap teardown in try/catch so cleanup errors do not mark otherwise-successful items postscript_failed.

Complete Example

Next Steps

Last modified on August 4, 2026