Skip to main content

What are Trace Groups?

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.

Why Use Trace Groups?

Multi-Turn Conversations

Track all questions and answers in a conversation thread

User Sessions

Group all AI interactions within a single user session

Email Threads

Link related email responses together

User Journeys

Follow a user’s complete journey through your application

Basic Usage

To create traces in a group, simply pass the same groupId to each trace:
from artanis import Artanis

artanis = Artanis(api_key="sk_...")

# First question in a conversation
trace1 = 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 conversation
trace2 = 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...")

Real-World Example: Chat Application

Here’s a complete example of using trace groups in a chat application:
from artanis import Artanis
from datetime import datetime

artanis = 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

# Usage
bot = ChatBot(user_id="user-123")

# All these traces will be grouped together
bot.answer("What is AI?")
bot.answer("How does machine learning work?")
bot.answer("Can you give me an example?")

Choosing Good Group IDs

Group IDs should be:
Unique
requirement
Each conversation/session should have a unique ID
Consistent
requirement
Use the same format across your application (e.g., conversation_, session_, thread_)
Meaningful
recommendation
Include context that helps you identify the group (e.g., user ID, timestamp)

Examples of Good Group IDs

# Conversation threads
group_id = f"conversation_{user_id}_{timestamp}"

# User sessions
group_id = f"session_{session_id}"

# Email threads
group_id = f"email_thread_{thread_id}"

# Support tickets
group_id = f"ticket_{ticket_number}"

Platform Benefits

When traces share a groupId, the Artanis platform provides:
View the full conversation flow to understand how the AI performed across multiple turns.
Analyze patterns across entire user sessions, not just individual traces.
When investigating an issue, see all related traces to understand the full context.

Best Practices

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.

Advanced: Dynamic Grouping

You can create groups dynamically based on your application logic:
from artanis import Artanis

artanis = 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 grouped
process_user_request("user-123", "report", {"date": "2024-01-15"})
process_user_request("user-123", "report", {"date": "2024-01-16"})

Next Steps