Manual Usage

If your application requires more flexibility and you can't use the wrap helpers, you can use the trackEvent method directly.

The trackEvent method is used to track and log runs in your application. It takes three parameters: type, event, and data.

Parameters:

ParameterTypeDescription
typeagent, tool, llm or chainThe type of the run
eventstart, end, error or feedbackThe name of the event.
dataPartial<RunEvent>Data associated with the run.

The RunEvent type is composed of the following properties:

FieldTypeDescriptionRequired
runIdstringUnique identifierYes
inputJSONInput dataNo
outputJSONOutput dataNo
tokensUsage{ completion: number, prompt: number }Number of tokens used in the run.No
userIdstringThe user ID.No
userPropsJSONThe user properties.No
parentRunIdstringThe parent run ID.No
extraJSONAny extra data associated with the event.No
tagsstring[]Tags associated with the event.No
error{ message: string, stack?: string }Error object if an error occurred.No

Example to track an agent:

// Assuming you have an instance of Lunary
// Define your agent function
async function myAgentFunction(input: string): Promise<string> {
// Start of the agent function
const runId = 'unique_run_id'; // Replace with a unique run ID for each run
lunary.trackEvent('agent', 'start', {
runId,
input,
name: 'MySuperAgent',
extra: { extra: 'data' },
tags: ['tag1', 'tag2']
});
try {
const output = await someAsyncOperation(input);
lunary.trackEvent('agent', 'end', {
runId,
output,
});
return output;
} catch (error) {
lunary.trackEvent('agent', 'error', {
runId,
error: error.message, // Replace with actual error message
name: 'myAgentFunction'
})
}
}
// Use the agent function
myAgentFunction('some input')
.then(output => console.log('Output:', output))
.catch(error => console.error('Error:', error));

Using the parentRunId parameter

To create traces with sub-agents, you can use the parentRunId parameter to link child runs.

async function someTool(input: string, parentRunId: string): Promise<string> {
const subRunId = 'sub_run_id';
lunary.trackEvent('tool', 'start', {
runId: subRunId,
parentRunId,
input,
name: 'MyTool',
tags: ['tag1', 'tag2']
});
try {
const output = await someAsyncOperation(input);
lunary.trackEvent('tool', 'end', {
runId: subRunId,
parentRunId,
output,
});
return output;
} catch (error) {
lunary.trackEvent('tool', 'error', {
runId: subRunId,
parentRunId,
error: error.message, // Replace with actual error message
name: 'mySubAgentFunction'
});
}
}
// Use the main agent function
myAgentFunction('some input')
.then(output => console.log('Output:', output))
.catch(error => console.error('Error:', error));

Questions? We're here to help.