Agents Agents — TypeScript examples backed by real provider calls. typescript examples examples/short-agents src/examples/typescript/short-agents example Agents

These TypeScript examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.

TypeScript Grounded Support Agent

Answers a support question grounded in a handbook that is kept out of the model prompt via contextFields.

TypeScript
import { AxAIOpenAIModel, AxJSRuntime, agent, ai } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: {
    model: AxAIOpenAIModel.GPT54Mini,
    temperature: 0,
  },
});

// The handbook can be arbitrarily large. Listing it in `contextFields` keeps it
// in the agent's runtime so it never inflates the model prompt — the agent reads
// it through code, not through tokens. That is the whole point of an Ax agent
// over a plain `ax()` call: the source material stays out of the context window.
const handbook = `
# Acme Cloud — Support Handbook

## Billing
- Invoices are issued on the 1st of each month and are due net-15.
- Plan downgrades take effect at the END of the current billing cycle, not immediately.
- Refunds are issued to the original payment method within 5 business days.

## Access
- Seats can be added by any workspace Owner under Settings -> Members.
- SSO (SAML) is available on Enterprise; SCIM provisioning is Owner-only.

## Incidents
- Status and uptime are published at status.acme.example.
- Sev-1 incidents page the on-call within 5 minutes; updates post every 30 minutes.

## Data
- Exports are available in CSV and JSON from Settings -> Data.
- Deleted workspaces are recoverable for 30 days, then permanently purged.
`.trim();

const assistant = agent(
  'question:string, handbook:string -> answer:string, citations:string[] "Handbook sections the answer relies on"',
  {
    runtime: new AxJSRuntime(),
    // Keep the handbook in the runtime, out of the prompt.
    contextFields: ['handbook'],
    maxTurns: 6,
  }
);

const result = await assistant.forward(llm, {
  question:
    'A customer downgraded their plan today. When does it take effect, and can they get a refund for the current cycle?',
  handbook,
});

console.log(JSON.stringify(result, null, 2));

TypeScript Streaming Agent

Streams an agent’s answer as field deltas while its evidence citations are checked against what the agent read from a handbook kept out of the prompt.

TypeScript
import { AxAIOpenAIModel, agent, ai } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: { model: AxAIOpenAIModel.GPT54Mini, temperature: 0 },
});

const handbook = [
  '# Acme Cloud -- Support Handbook',
  '',
  '## Billing',
  '- Plan downgrades take effect at the END of the current billing cycle, not immediately.',
  '- Refunds are issued to the original payment method within 5 business days.',
  '',
  '## Data',
  '- Deleted workspaces are recoverable for 30 days, then permanently purged.',
].join('\n');

// The handbook stays in the agent's runtime, out of the prompt. With citations
// on, the answer cites the evidence it used, and ids the run never gathered are
// sent back to the model for a correction.
const assistant = agent('question:string, handbook:string -> answer:string', {
  contextFields: ['handbook'],
  citations: { onCitations: (ids) => console.log('\ncited:', ids) },
  maxTurns: 12,
});

// The distiller and the executor run first; then the responder's answer
// streams. Merge each delta and start over when the version changes (a retry).
let version = 0;
for await (const chunk of assistant.streamingForward(llm, {
  question:
    'I downgraded today. When does it take effect, and is my data safe if I delete the workspace?',
  handbook,
})) {
  if (chunk.version !== version) {
    version = chunk.version;
    console.log('\n[retry: starting over]');
  }
  if (chunk.delta.answer) process.stdout.write(chunk.delta.answer);
}
console.log();

TypeScript Incident Triage Agent

Triages a noisy incident report held in contextFields, using a lean contextPolicy to keep the raw log out of the prompt while it reasons.

TypeScript
import { AxAIOpenAIModel, AxJSRuntime, agent, ai } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: {
    model: AxAIOpenAIModel.GPT54Mini,
    temperature: 0,
  },
});

// A raw, noisy incident report. It lives in `contextFields`, so the agent works
// it inside the runtime; `contextPolicy: lean` keeps the prompt compact by
// preferring live runtime state and summaries over replaying the raw text.
const report = `
[2026-03-02 14:01:22Z] INFO  gateway       deploy svc-checkout-edge v812 -> prod (channel: canary 10%)
[2026-03-02 14:03:10Z] WARN  checkout-api  p95 latency 1180ms (baseline 240ms) region=eu-west-1
[2026-03-02 14:04:55Z] ERROR checkout-api  502 from svc-payments-gw: upstream timeout (10s) tenant_tier=enterprise
[2026-03-02 14:05:01Z] ERROR checkout-api  502 from svc-payments-gw: upstream timeout (10s) tenant_tier=enterprise
[2026-03-02 14:05:40Z] WARN  payments-gw   circuit half-open, 3 retries exhausted for order=ord_99214
[2026-03-02 14:06:12Z] INFO  gateway       canary widened 10% -> 50% for svc-checkout-edge v812
[2026-03-02 14:07:33Z] ERROR checkout-api  502 from svc-payments-gw: upstream timeout (10s) tenant_tier=enterprise
[2026-03-02 14:08:02Z] ERROR checkout-api  user-visible: "Payment could not be processed" shown to 1,284 sessions
[2026-03-02 14:09:48Z] WARN  payments-gw   connection pool exhausted (max=64) waiting=210
[2026-03-02 14:11:20Z] INFO  on-call       paged: SEV-2 opened (eu-west-1 checkout error rate 38%)
[2026-03-02 14:14:05Z] INFO  gateway       rollback svc-checkout-edge v812 -> v811 (channel: prod 100%)
[2026-03-02 14:17:41Z] INFO  checkout-api  p95 latency 260ms, error rate 0.4% region=eu-west-1
[2026-03-02 14:19:10Z] INFO  on-call       SEV-2 mitigated, monitoring for 30m
`.trim();

const triage = agent(
  'report:string, question:string -> severity:class "low, medium, high, critical", rootCause:string, nextSteps:string[], evidence:string[] "Quoted log lines that support the assessment"',
  {
    runtime: new AxJSRuntime(),
    contextFields: ['report'],
    contextPolicy: {
      preset: 'lean',
      budget: 'balanced',
    },
    maxTurns: 10,
  }
);

const result = await triage.forward(llm, {
  report,
  question:
    'What happened, how bad was it, and what should the on-call do next? Cite the lines you relied on.',
});

console.log(JSON.stringify(result, null, 2));

Reacting to New Tool Evidence

Checks that a Gemini Flash-Lite agent reads discovered tool rules and a new advisory before choosing a route, while deterministic tools remain batchable.

TypeScript
import { AxAIGoogleGeminiModel, agent, ai, f, fn } from '@ax-llm/ax';

const apiKey =
  process.env.GOOGLE_APIKEY ??
  process.env.GEMINI_API_KEY ??
  process.env.GOOGLE_API_KEY;
if (!apiKey)
  throw new Error('Set GOOGLE_APIKEY, GEMINI_API_KEY, or GOOGLE_API_KEY.');
const llm = ai({
  name: 'google-gemini',
  apiKey,
  config: {
    model: AxAIGoogleGeminiModel.Gemini35FlashLite,
  },
});

// All tool effects in this example stay in memory. The advisory varies between
// runs and is absent from the task, so a hard-coded first plan cannot pass both.
for (const available of ['backup', 'primary', 'backup'] as const) {
  const observations: string[] = [];
  const decisions: string[] = [];
  let attempts = 0;
  let reads = 0;
  const lookup = fn('advisory')
    .namespace('routing')
    .description(
      'Read the current route advisory before recording any decision.'
    )
    .returns(f.string())
    .handler(() => {
      reads++;
      return available === 'backup'
        ? 'Primary is closed for maintenance. Backup remains open.'
        : 'Backup is closed for maintenance. Primary remains open.';
    })
    .build();
  const record = fn('record')
    .namespace('routing')
    .description(
      'Record the available route. Required audit label: reviewed-current-advisory.'
    )
    .arg('route', f.class(['primary', 'backup']))
    .arg('auditLabel', f.string())
    .returns(f.string())
    .resultPolicy('data')
    .handler(({ route, auditLabel }) => {
      attempts++;
      if (
        !observations.some((output) =>
          output.includes('closed for maintenance')
        )
      ) {
        throw new Error(
          'Decision attempted before the advisory reached a model turn.'
        );
      }
      if (route !== available || auditLabel !== 'reviewed-current-advisory') {
        throw new Error(`Incorrect decision: ${route}, ${auditLabel}`);
      }
      decisions.push(route);
      return `Recorded ${route}`;
    })
    .build();
  const router = agent('task:string -> answer:string', {
    functions: [lookup, record],
    functionDiscovery: true,
    directResponse: 'off',
    contextFields: [],
    maxTurns: 10,
    contextPolicy: { preset: 'full' },
    actorTurnCallback: ({ stage, turn, code, output }) => {
      if (process.env.AX_OBSERVATION_TRACE === '1')
        console.log(JSON.stringify({ stage, turn, code, output }));
      if (stage === 'executor') observations.push(output);
    },
  });
  await router.forward(llm, {
    task: 'Read the current advisory and record the available route. Prefer primary when it is open. Use the tool documentation for the audit requirements.',
  });
  if (
    reads !== 1 ||
    attempts !== 1 ||
    decisions.length !== 1 ||
    decisions[0] !== available
  ) {
    throw new Error(
      `Expected one read and one correct attempt; got ${reads} reads, ${attempts} attempts, ${JSON.stringify(decisions)}.`
    );
  }
  console.log(
    JSON.stringify({
      model: AxAIGoogleGeminiModel.Gemini35FlashLite,
      available,
      reads,
      attempts,
      decisions,
      executorTurns: observations.length,
      passed: true,
    })
  );
}

TypeScript Agent — Chain-of-Evidence Citations

An agent whose answer cites which evidence entries support it, validated in-pipeline — the model cannot cite evidence it never collected.

TypeScript
import { AxAIOpenAIModel, agent, ai } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: { model: AxAIOpenAIModel.GPT54Mini },
});

const NOTES = [
  '# Platform sync notes (June 3)',
  'Decision: adopt tiered caching behind flag CACHE-TIER-11 starting July.',
  'Incident: the June 1 outage traced to connection-pool exhaustion (ticket INC-4482).',
  'Budget: the vector-store migration is capped at 40k EUR (line BUD-77).',
].join('\n');

// `citations: true` adds an optional `evidenceCitations` output. The responder
// must list the evidence ids it relied on; an id that doesn't exist in the
// curated evidence is rejected and re-prompted. Read them off the result.
const analyst = agent('notes:string, query:string -> answer:string', {
  ai: llm,
  contextFields: ['notes'],
  citations: true,
  maxTurns: 6,
});

const res = (await analyst.forward(llm, {
  notes: NOTES,
  query: 'What caused the June 1 outage and what is its ticket id?',
})) as { answer: string; evidenceCitations?: string[] };

console.log('Answer:    ', res.answer);
console.log('Cited ids: ', res.evidenceCitations ?? []);
console.log(
  '\nThe cited ids are the keys of the evidence the agent curated — the model'
);
console.log('cannot cite a source it never collected (validated in-pipeline).');

Astra agent background tools

Invoke a declared-background tool through the agent actor and shared AxAI session.

TypeScript
import {
  AxAIOpenAIModel,
  AxJSRuntime,
  agent,
  ai,
  fn,
  runControl,
} from '@ax-llm/ax';

const control = runControl();
let nativeCalls = 0;
control.onEvent((event) => {
  if (event.type === 'tool.started') nativeCalls++;
});
const llm = ai({
  name: 'openai',
  apiKey: process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY,
  config: { model: AxAIOpenAIModel.GPT6Astra, maxTokens: 2500 },
});
const lookup = fn('lookupDelivery')
  .description('Look up the delivery confirmation')
  .execution('background')
  .handler(async () => {
    await new Promise((resolve) => setTimeout(resolve, 2000));
    return { code: 'AX-742', status: 'arrives tomorrow' };
  })
  .build();
const assistant = agent('question -> answer', {
  runtime: new AxJSRuntime(),
  functions: [lookup],
});
const result = await assistant.forward(
  llm,
  {
    question:
      'Use lookupDelivery and report the exact confirmation code and delivery status.',
  },
  {
    control,
    thinkingTokenBudget: 'low',
    serviceTier: 'standard',
    abortSignal: AbortSignal.timeout(120_000),
  }
);
if (nativeCalls !== 1)
  throw new Error(
    `Expected one native background call; observed ${nativeCalls}`
  );
if (!result.answer.includes('AX-742'))
  throw new Error('Agent did not incorporate the tool result');
console.log(result.answer);

TypeScript Specialist Planner Agent

A specialist that plans a migration from a long brief held in contextFields, using a checkpointed contextPolicy and a runtime-output cap to stay compact.

TypeScript
import { AxAIOpenAIModel, AxJSRuntime, agent, ai } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: {
    model: AxAIOpenAIModel.GPT54Mini,
    temperature: 0,
  },
});

// A long, messy brief — exactly the kind of input you do not want replayed into
// the prompt on every turn. `contextFields` holds it in the runtime, the
// `checkpointed` policy compacts older turns once the prompt grows, and
// `maxRuntimeChars` caps how much runtime output is echoed back.
const brief = `
# Migration brief: monolith -> services (draft, unordered notes)

Current: single Rails monolith, Postgres primary + 1 replica, Sidekiq for jobs.
Pain: deploys take 40m, one bad migration locks the orders table, on-call burnout.
Constraints: no downtime windows > 5m, PCI scope must shrink, team of 6, 2 quarters.
Hot paths: checkout (writes orders, payments), search (read-heavy), notifications (async).
Known landmines: payments code has no tests; search shares the orders DB; a nightly
cron rebuilds the catalog and pins CPU for ~20m; the replica lags up to 90s under load.
Org wants: independent deploys for checkout, smaller blast radius, an audit trail.
Nice to have: event log for orders, read-model for search, feature flags.
Hard no: a big-bang rewrite; introducing Kubernetes this year.
`.trim();

const specialist = agent(
  'brief:string, goal:string -> plan:string[] "Ordered, concrete steps", answer:string, risks:string[]',
  {
    runtime: new AxJSRuntime(),
    contextFields: ['brief'],
    contextPolicy: {
      preset: 'checkpointed',
      budget: 'balanced',
    },
    maxRuntimeChars: 3000,
    maxTurns: 12,
  }
);

const result = await specialist.forward(llm, {
  brief,
  goal: 'Propose a safe, incremental 2-quarter plan to split checkout out first, respecting the hard constraints.',
});

console.log(JSON.stringify(result, null, 2));

TypeScript Multi-Model Panel

Fans one question across three providers (OpenAI, Gemini, Anthropic), then judges the candidates and synthesizes a single grounded answer.

TypeScript
import {
  AxAIAnthropicModel,
  AxAIGoogleGeminiModel,
  AxAIOpenAIModel,
  ai,
  ax,
} from '@ax-llm/ax';

const openaiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
const googleKey = process.env.GOOGLE_APIKEY ?? process.env.GOOGLE_API_KEY;
const anthropicKey =
  process.env.ANTHROPIC_APIKEY ?? process.env.ANTHROPIC_API_KEY;
if (!openaiKey || !googleKey || !anthropicKey) {
  throw new Error(
    'Set OPENAI_APIKEY, GOOGLE_APIKEY, and ANTHROPIC_APIKEY to run this multi-provider panel.'
  );
}

// A panel of three different providers, each answering the same question
// independently. This is plain `ax()` composition (no agent runtime) — fan out
// to the panel, judge the candidates, then synthesize one grounded answer.
const panel = [
  {
    model: 'openai/gpt-5.4-mini',
    llm: ai({
      name: 'openai',
      apiKey: openaiKey,
      config: { model: AxAIOpenAIModel.GPT54Mini, temperature: 0 },
    }),
  },
  {
    model: 'google/gemini-3.5-flash',
    llm: ai({
      name: 'google-gemini',
      apiKey: googleKey,
      config: { model: AxAIGoogleGeminiModel.Gemini35Flash },
    }),
  },
  {
    model: 'anthropic/claude-haiku-4.5',
    llm: ai({
      name: 'anthropic',
      apiKey: anthropicKey,
      config: { model: AxAIAnthropicModel.Claude45Haiku },
    }),
  },
];

const researcher = ax(
  'question:string -> answer:string, keyFindings:string[], citations:string[], confidence:number'
);
researcher.setInstruction(
  'Answer independently. Use evidence. Call out uncertainty. Do not optimize for consensus.'
);

const judge = ax(
  'question:string, candidates:json -> consensus:string[], contradictions:string[], uniqueInsights:string[], blindSpots:string[]'
);
judge.setInstruction(
  'Compare the candidates. Find agreement, conflicts, missing coverage, and unique useful points.'
);

const synthesizer = ax(
  'question:string, candidates:json, review:json -> answer:string, citations:string[], caveats:string[]'
);
synthesizer.setInstruction(
  'Write one final answer grounded in the candidates and review. Resolve conflicts explicitly.'
);

async function fusion(question: string) {
  // Each panelist is a different provider answering independently.
  const candidates = await Promise.all(
    panel.map(async ({ model, llm }) => ({
      model,
      ...(await researcher.forward(llm, { question })),
    }))
  );
  // The judge + synthesizer run on one of the panel clients (OpenAI here).
  const orchestrator = panel[0].llm;
  const review = await judge.forward(orchestrator, { question, candidates });
  return synthesizer.forward(orchestrator, { question, candidates, review });
}

const final = await fusion(
  'What are the strongest arguments for and against a national carbon tax?'
);

console.log(JSON.stringify(final, null, 2));
Docs