Wrap your agent
One wrap records one run. Every option on the wrap, everything the run handle can set, and when one wrap is the wrong shape.
wrapAgent turns one call of your agent into one run. Model calls made inside it, and any spans you add, nest under that run.
import trodo from 'trodo-node';
trodo.init({ siteId: process.env.TRODO_SITE_ID! });
const { result, runId } = await trodo.wrapAgent(
'support-agent',
async (run) => {
run.setInput(question);
const answer = await agent.run(question);
run.setOutput(answer);
return answer;
},
{
distinctId: user.id,
conversationId: thread.id,
metadata: { plan: user.plan, release: process.env.GIT_SHA },
},
);import os, trodo
trodo.init(site_id=os.environ["TRODO_SITE_ID"])
with trodo.wrap_agent(
"support-agent",
distinct_id=user.id,
conversation_id=thread.id,
metadata={"plan": user.plan, "release": os.environ.get("GIT_SHA")},
) as run:
run.set_input(question)
answer = agent.run(question)
run.set_output(answer)
run_id = run.run_idIn async code use async with trodo.wrap_agent(...) as run:.
The wrap returns your function's result and the run's ID. If your function throws, the run is recorded with status error and the error is re-thrown to you (Status & errors).
Options
| Node | Python | What it does |
|---|---|---|
distinctId | distinct_id | The user the run was for. See Users. |
conversationId | conversation_id | The chat or thread the run is one turn of. See Conversations. |
metadata | metadata | Key/values you want to filter and group runs by. See Metadata. |
user | user | Properties to write on the user's profile in the same call, e.g. { properties: { plan: 'pro' } }. See Users. |
parentRunId | parent_run_id | Links this run to the run that started it, for an agent handed work by another. See Multi-agent. |
The run handle
Your function gets a run handle. Everything on it is optional.
| Node | Python | What it does |
|---|---|---|
setInput(value) | set_input(value) | The run's input: the user's question or the request. A string, an object, or a list of chat messages. |
setOutput(value) | set_output(value) | The run's final answer. Set the answer itself, not an HTTP response or a stream handle. |
setMetadata({...}) | set_metadata({...}) | Adds metadata known only once the run is under way. |
setErrorSummary(text, { type }) | set_error_summary(text, type=) | Marks the run failed without throwing, for an agent that catches its own errors and returns a polite reply. |
Set input and output on every run: evals, Lucid and the run list all read them.
One run per request
A wrap opens a new top-level run every time, and a wrap inside a wrap makes two separate runs, not a parent and a child. So:
- Wrap the entry point that answers one request, not each model call, tool or loop step inside it. Those become spans of the run on their own, or with manual spans.
- A sub-agent inside the request is an
agentspan, not another wrap. See Multi-agent. - Work that outlives the request (a queue job, a long chat session over many requests) needs
startRunandendRuninstead.
Decorator (Python)
For a function whose user and conversation don't change between calls, a decorator does the same as the with block. The function's return value becomes the run's output; set the input yourself if you need it.
@trodo.agent("nightly-report")
def build_report(day):
...Works on async def functions too. When each call is for a different user or conversation, use wrap_agent so you can pass their IDs.
Turning auto-capture off
init registers an instrumentor for every supported provider and framework it finds installed. Turn one off when two capture the same call (for example LangChain over OpenAI), or all of them:
trodo.init({ siteId, disableInstrumentations: ['openai'] }); // one
trodo.init({ siteId, autoInstrument: false }); // alltrodo.init(site_id=site_id, disable_instrumentations=["openai"]) # one
trodo.init(site_id=site_id, auto_instrument=False) # all