Trace Groups allow you to link related traces together by assigning them a common groupId. This is useful for tracking multi-turn conversations, user sessions, email threads, or any workflow where multiple AI operations are connected.When traces share the same groupId, the Artanis platform displays them in a Related Traces section, making it easy to navigate between connected operations and understand the full context of a user’s journey.
To create traces in a group, simply pass the same groupId to each trace:
Copy
from artanis import Artanisartanis = Artanis(api_key="sk_...")# First question in a conversationtrace1 = artanis.trace( "answer-question", metadata={"user_id": "user-123"}, group_id="conversation_abc123")trace1.input(question="What is AI?")trace1.output("AI stands for Artificial Intelligence...")# Follow-up question in the same conversationtrace2 = artanis.trace( "answer-question", metadata={"user_id": "user-123"}, group_id="conversation_abc123" # Same groupId)trace2.input(question="How does it work?")trace2.output("AI works by processing data through algorithms...")
Here’s a complete example of using trace groups in a chat application:
Copy
from artanis import Artanisfrom datetime import datetimeartanis = Artanis(api_key="sk_...")class ChatBot: def __init__(self, user_id: str): self.user_id = user_id # Generate a unique conversation ID self.conversation_id = f"conv_{user_id}_{datetime.now().isoformat()}" def answer(self, question: str) -> str: # Create a trace for this question trace = artanis.trace( "answer-question", metadata={ "user_id": self.user_id, "timestamp": datetime.now().isoformat() }, group_id=self.conversation_id # Link to conversation ) trace.input(question=question) # Get answer from your AI model answer = your_ai_model.generate(question) trace.output(answer) return answer# Usagebot = ChatBot(user_id="user-123")# All these traces will be grouped togetherbot.answer("What is AI?")bot.answer("How does machine learning work?")bot.answer("Can you give me an example?")
Don’t reuse group IDs across different users or unrelated workflows. Each group
should represent a single logical unit (conversation, session, thread, etc.).
Store the group ID alongside your conversation/session data so you can always
link back to the traces in Artanis.
The groupId is optional. Only use it when you have related traces to link
together. For one-off operations, you can omit it.
You can create groups dynamically based on your application logic:
Copy
from artanis import Artanisartanis = Artanis(api_key="sk_...")def process_user_request(user_id: str, request_type: str, data: dict): # Group by user and request type group_id = f"{user_id}_{request_type}_{datetime.now().date()}" trace = artanis.trace( f"process-{request_type}", metadata={"user_id": user_id, "type": request_type}, group_id=group_id ) trace.input(**data) result = your_processor.process(request_type, data) trace.output(result) return result# All "report" requests from user-123 today will be groupedprocess_user_request("user-123", "report", {"date": "2024-01-15"})process_user_request("user-123", "report", {"date": "2024-01-16"})