> For the complete documentation index, see [llms.txt](https://docs.algenta.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.algenta.ai/sdks/typescript/simulations-and-queries.md).

# Simulations & queries

These methods live on `AlgentaClient` and cover two families from the simulation and query surface: running decisions under uncertainty (`simulate`, `planDecision`, `recommend`, `score`, `batch`, `compare`) and reading governed data (`resolve`, `query`, `queryWithMetadata`, `verify`, `queryBatch`, `querySqlReport`, `explain`). Method names are `camelCase`; bodies stay `snake_case`.

## Simulation methods

A `SimulateRequest` in `auto` mode describes a scenario with named `variables` (each a `low`/`high` range) and an `objective`, and the engine returns a `DecisionEnvelope` with the expected value, percentiles, and a recommended action.

```typescript
import { AlgentaClient } from "algenta-sdk";

const client = new AlgentaClient({ apiKey: process.env.ALGENTA_API_KEY });

// Run one decision.
const envelope = await client.simulate({
  mode: "auto",
  scenario: {
    name: "Product launch decision",
    variables: {
      revenue: { low: 80000, high: 200000 },
      cost: { low: 40000, high: 90000 },
    },
    objective: "maximize_net_value",
  },
  runs: 10000,
  seed: 42,
});
console.log(envelope.recommended_action, envelope.confidence);

// Compare two named scenarios in one call.
const comparison = await client.compare(
  [
    { name: "launch_now", request: { mode: "auto", scenario: { variables: { revenue: { low: 80000, high: 200000 } }, objective: "maximize_net_value" } } },
    { name: "wait_a_quarter", request: { mode: "auto", scenario: { variables: { revenue: { low: 60000, high: 150000 } }, objective: "maximize_net_value" } } },
  ],
  { runs: 5000, seed: 42 },
);
console.log(comparison);
```

| Method                            | HTTP                      | Notes                                                                            |
| --------------------------------- | ------------------------- | -------------------------------------------------------------------------------- |
| `simulate(request)`               | `POST /v1/simulate`       | Runs one `SimulateRequest`; returns a `DecisionEnvelope`.                        |
| `planDecision(request)`           | `POST /v1/decisions/plan` | Returns a structured `DecisionPlanResponse` for the same request shape.          |
| `recommend(actions, options?)`    | `POST /v1/recommend`      | `actions` is an array of `{ name, request }`; `options` sets `{ runs, seed }`.   |
| `score(request, scoringWeights?)` | `POST /v1/score`          | Scores one request; `scoringWeights` maps to `scoring_weights`.                  |
| `batch(items)`                    | `POST /v1/batch`          | Runs an array of `SimulateRequest` in one call; returns a `BatchResult`.         |
| `compare(scenarios, options?)`    | `POST /v1/compare`        | `scenarios` is an array of `{ name, request }`; `options` sets `{ runs, seed }`. |

{% hint style="info" %}
`recommend` and `compare` share the same shape — an array of named requests plus optional `{ runs, seed }`. Use `recommend` to pick a single best action and `compare` to rank named scenarios side by side.
{% endhint %}

## Query methods

The query family is the governed data path: deterministic, typed reads over onboarded datasets. `queryWithMetadata` returns `{ data, metadata, headers }` so you get the response alongside its execution metadata and raw headers; `querySqlReport` runs a read-only SQL report over one or more aliased datasets.

```typescript
// Aggregate one metric, with execution metadata.
const result = await client.queryWithMetadata({
  source_name: "nps_scores",
  metric_column: "nps_score",
  group_column: "origin_airport_code",
  aggregation: "avg",
});
console.log(result.data, result.metadata, result.headers);

// Read-only SQL report across two aliased datasets.
const report = await client.querySqlReport({
  sources: [
    { dataset_id: "ds_nps_scores", alias: "nps" },
    { dataset_id: "ds_flights", alias: "flights" },
  ],
  sql: "SELECT flights.origin_airport_code, AVG(nps.nps_score) AS avg_nps FROM nps JOIN flights ON nps.flight_id = flights.flight_id GROUP BY 1",
  max_rows: 500,
});
console.log(report.columns, report.row_count, report.truncated);
```

| Method                       | HTTP                        | Notes                                                                               |
| ---------------------------- | --------------------------- | ----------------------------------------------------------------------------------- |
| `resolve(request)`           | `POST /v1/resolve`          | Resolves a query hint to a concrete source/column plan.                             |
| `query(request)`             | `POST /v1/query`            | Runs a governed query; returns a `QueryResponse`.                                   |
| `queryWithMetadata(request)` | `POST /v1/query`            | Same call, returning `{ data, metadata, headers }`.                                 |
| `verify(request)`            | `POST /v1/verify`           | Verifies a query resolves and validates before you run it.                          |
| `queryBatch(request)`        | `POST /v1/query/batch`      | Runs keyed `queries` with optional shared `defaults`.                               |
| `querySqlReport(request)`    | `POST /v1/query/sql-report` | Read-only SQL over aliased `sources`; `max_rows` caps output.                       |
| `explain(request)`           | `POST /v1/query`            | Runs the query and returns an `ExplainResponse` (source set, join path, plan hash). |

{% hint style="success" %}
Governed queries are deterministic: the same request against the same dataset returns the same rows in the same order. Use `verify` (or `explain`) to check the resolved plan before running a query for the first time.
{% endhint %}

## Next steps

<table data-view="cards"><thead><tr><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Log decisions and call products</strong></td><td>Persist decision memory and use the product endpoints.</td><td><a href="/pages/ONhCwMfELeN1mFWHwnXk">/pages/ONhCwMfELeN1mFWHwnXk</a></td></tr><tr><td><strong>Run a governed query end to end</strong></td><td>Onboard data and query it under a typed contract.</td><td><a href="/pages/1hWq4uaSOQ9pNlD9Ox0f">/pages/1hWq4uaSOQ9pNlD9Ox0f</a></td></tr><tr><td><strong>Set up the client</strong></td><td>Install, configure, and handle errors.</td><td><a href="/pages/94zbz2BEgat0o5aXo4iU">/pages/94zbz2BEgat0o5aXo4iU</a></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.algenta.ai/sdks/typescript/simulations-and-queries.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
