Overview
Learn how to implement AI tracking using Handit.AI's wrapper-based approach.
Basic Implementation
import { startAgentTracing, traceAgentNode } from '@handit.ai/node';
import { OpenAI } from 'openai';
const openai = new OpenAI();
// Define your agent function
async function myAgent(input) {
// Define a node (e.g., GPT call)
const gptNode = async (messages) => {
const response = await openai.chat.completions.create({
messages,
model: "gpt-4"
});
return response;
};
// Wrap node with tracing
const tracedGpt = traceAgentNode({
agentNodeId: agentsTrackingConfig.mariaManager.documentCompressor,
callback: gptNode
});
// Execute traced node
return await tracedGpt(input);
}
// Wrap entire agent with tracing
const tracedAgent = startAgentTracing(myAgent);
// Use the agent
const result = await tracedAgent({
messages: [{ role: "user", content: "Hello!" }]
});
from handit import start_agent_tracing, trace_agent_node
from openai import OpenAI
class MyAgent:
def __init__(self):
self.client = OpenAI(api_key="your-api-key")
@start_agent_tracing
async def process(self, input_data):
# Define node execution (e.g., GPT call)
async def execute_gpt_node(messages):
response = await self.client.chat.completions.create(
messages=messages,
model="gpt-4"
)
return response
# Wrap node execution with tracing
traced_gpt = trace_agent_node({
"agent_node_id": agent_config["mar_i_a_manager"]["document_compressor"],
"callback": execute_gpt_node
})
# Execute node with tracing
return await traced_gpt(input_data)
# Use the agent
agent = MyAgent()
response = await agent.process({
"messages": [{"role": "user", "content": "Hello!"}]
})
Advanced Usage
Multiple Node Tracing
Track multiple nodes within a single agent:
async function complexAgent(input) {
// First node: Document compression
const compressNode = traceAgentNode({
agentNodeId: agentsTrackingConfig.mariaManager.documentCompressor,
callback: async (doc) => {/* compression logic */}
});
// Second node: Task assignment
const assignNode = traceAgentNode({
agentNodeId: agentsTrackingConfig.mariaManager.taskAssigner,
callback: async (task) => {/* assignment logic */}
});
const compressed = await compressNode(input);
const assigned = await assignNode(compressed);
return assigned;
}
const tracedComplexAgent = startAgentTracing(complexAgent);
class ComplexAgent:
@start_agent_tracing
async def process(self, input_data):
# First node: Document compression
compress_node = trace_agent_node({
"agent_node_id": agent_config["mar_i_a_manager"]["document_compressor"],
"callback": self.compress_document
})
# Second node: Task assignment
assign_node = trace_agent_node({
"agent_node_id": agent_config["mar_i_a_manager"]["task_assigner"],
"callback": self.assign_task
})
compressed = await compress_node(input_data)
assigned = await assign_node(compressed)
return assigned
Tracking AI Tools
Software Tools
APIs
Databases
External AI models
Non-Software Tools
Hardware sensors
Human escalations
Last updated