Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.trodo.ai/docs/llms.txt

Use this file to discover all available pages before exploring further.

Break a task into discrete stages and render each as its own named span. No framework needed — just withSpan with kind='agent' for each stage.
import trodo from 'trodo-node';
import OpenAI from 'openai';

trodo.init({ siteId: process.env.TRODO_SITE_ID });
const openai = new OpenAI();

async function triage(question) {
  return trodo.withSpan({ kind: 'agent', name: 'triage' }, async (span) => {
    span.setInput({ question });
    const r = await openai.chat.completions.create({
      model: 'gpt-4o-mini',
      messages: [{ role: 'user', content: `Classify intent: ${question}` }],
    });
    const intent = r.choices[0].message.content.trim();
    span.setOutput({ intent });
    return intent;
  });
}

async function research(intent, question) {
  return trodo.withSpan({ kind: 'agent', name: 'research' }, async (span) => {
    span.setInput({ intent, question });
    // Your retrieval / API calls here, auto-captured as child spans.
    const facts = await kb.search(question);
    span.setOutput({ factCount: facts.length });
    return facts;
  });
}

async function write(question, facts) {
  return trodo.withSpan({ kind: 'agent', name: 'write' }, async (span) => {
    span.setInput({ question, factCount: facts.length });
    const r = await openai.chat.completions.create({
      model: 'gpt-4o-mini',
      messages: [{ role: 'user', content: `Answer: ${question}\nFacts: ${JSON.stringify(facts)}` }],
    });
    const answer = r.choices[0].message.content;
    span.setOutput({ answer });
    return answer;
  });
}

export async function support(userId, question) {
  const { result } = await trodo.wrapAgent('multi-step', async (run) => {
    run.setInput({ question });

    const intent  = await triage(question);
    const facts   = await research(intent, question);
    const answer  = await write(question, facts);

    run.setOutput({ answer, intent });
    return answer;
  }, { distinctId: userId });

  return result;
}

Waterfall

run: multi-step
  ├─ agent triage
  │    └─ llm openai.chat.completions
  ├─ agent research
  │    └─ (your retrieval spans)
  └─ agent write
       └─ llm openai.chat.completions
kind='agent' spans render as stage headers in the dashboard; LLM / tool / retrieval spans nest underneath.

See also