The only tool you need to know what is happening and how to fix it.
Go alertingexception-trackerissue-trackinglogsmetricsmonitoringobservability-platformopen-telemetryotelsession-replaytelemetrytraces
mostly done
ddux3f399227ef2e
diff --git a/backend/app/controllers/ai_trace.controller.go b/backend/app/controllers/ai_trace.controller.gonew file mode 100644index 00000000..f141dc67--- /dev/null+++ b/backend/app/controllers/ai_trace.controller.go@@ -0,0 +1,164 @@+package controllers++import (+ "encoding/json"+ "net/http"+ "net/url"+ "time"++ "github.com/gin-gonic/gin"+ "github.com/google/uuid"+ "github.com/tracewayapp/traceway/backend/app/middleware"+ "github.com/tracewayapp/traceway/backend/app/models"+ "github.com/tracewayapp/traceway/backend/app/repositories"+ "github.com/tracewayapp/traceway/backend/app/storage"+ traceway "go.tracewayapp.com"+)++type aiTraceController struct{}++type AiTraceSearchRequest struct {+ FromDate time.Time `json:"fromDate"`+ ToDate time.Time `json:"toDate"`+ OrderBy string `json:"orderBy"`+ SortDirection string `json:"sortDirection"`+ Pagination PaginationParams `json:"pagination"`+ Search string `json:"search"`+}++type AiTraceInstancesRequest struct {+ FromDate time.Time `json:"fromDate"`+ ToDate time.Time `json:"toDate"`+ OrderBy string `json:"orderBy"`+ SortDirection string `json:"sortDirection"`+ Pagination PaginationParams `json:"pagination"`+}++type AiTraceInstancesResponse struct {+ Data []models.AiTrace `json:"data"`+ Stats *models.AiTraceDetailStats `json:"stats"`+ Pagination Pagination `json:"pagination"`+}++type AiTraceDetailResponse struct {+ AiTrace *models.AiTrace `json:"aiTrace"`+ Conversation json.RawMessage `json:"conversation,omitempty"`+}++func (a aiTraceController) FindGroupedByTraceName(c *gin.Context) {+ projectId, err := middleware.GetProjectId(c)+ if err != nil {+ c.AbortWithError(http.StatusInternalServerError, traceway.NewStackTraceErrorf("RequireProjectAccess middleware must be applied: %w", err))+ return+ }++ var request AiTraceSearchRequest+ if err := c.ShouldBindJSON(&request); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})+ return+ }++ stats, total, err := repositories.AiTraceRepository.FindGroupedByTraceName(c, projectId, request.FromDate, request.ToDate, request.Pagination.Page, request.Pagination.PageSize, request.OrderBy, request.SortDirection, request.Search)+ if err != nil {+ c.AbortWithError(500, traceway.NewStackTraceErrorf("error loading ai trace stats: %w", err))+ return+ }++ c.JSON(http.StatusOK, PaginatedResponse[models.AiTraceStats]{+ Data: stats,+ Pagination: Pagination{+ Page: request.Pagination.Page,+ PageSize: request.Pagination.PageSize,+ Total: total,+ TotalPages: (total + int64(request.Pagination.PageSize) - 1) / int64(request.Pagination.PageSize),+ },+ })+}++func (a aiTraceController) FindByTraceName(c *gin.Context) {+ projectId, err := middleware.GetProjectId(c)+ if err != nil {+ c.AbortWithError(http.StatusInternalServerError, traceway.NewStackTraceErrorf("RequireProjectAccess middleware must be applied: %w", err))+ return+ }++ rawTraceName := c.Query("traceName")+ if rawTraceName == "" {+ c.JSON(http.StatusBadRequest, gin.H{"error": "traceName is required"})+ return+ }++ traceName, err := url.PathUnescape(rawTraceName)+ if err != nil {+ traceName = rawTraceName+ }++ var request AiTraceInstancesRequest+ if err := c.ShouldBindJSON(&request); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})+ return+ }++ traces, total, err := repositories.AiTraceRepository.FindByTraceName(c, projectId, traceName, request.FromDate, request.ToDate, request.Pagination.Page, request.Pagination.PageSize, request.OrderBy, request.SortDirection)+ if err != nil {+ c.AbortWithError(500, traceway.NewStackTraceErrorf("error loading ai traces: %w", err))+ return+ }++ stats, err := repositories.AiTraceRepository.GetTraceNameStats(c, projectId, traceName, request.FromDate, request.ToDate)+ if err != nil {+ stats = nil+ }++ c.JSON(http.StatusOK, AiTraceInstancesResponse{+ Data: traces,+ Stats: stats,+ Pagination: Pagination{+ Page: request.Pagination.Page,+ PageSize: request.Pagination.PageSize,+ Total: total,+ TotalPages: (total + int64(request.Pagination.PageSize) - 1) / int64(request.Pagination.PageSize),+ },+ })+}++func (a aiTraceController) GetAiTraceDetail(c *gin.Context) {+ projectId, err := middleware.GetProjectId(c)+ if err != nil {+ c.AbortWithError(http.StatusInternalServerError, traceway.NewStackTraceErrorf("RequireProjectAccess middleware must be applied: %w", err))+ return+ }++ traceId, err := uuid.Parse(c.Param("traceId"))+ if err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid traceId"})+ return+ }++ aiTrace, err := repositories.AiTraceRepository.FindById(c, projectId, traceId)+ if err != nil {+ c.AbortWithError(http.StatusInternalServerError, traceway.NewStackTraceErrorf("error loading ai trace: %w", err))+ return+ }+ if aiTrace == nil {+ c.JSON(http.StatusNotFound, gin.H{"error": "AI trace not found"})+ return+ }++ response := AiTraceDetailResponse{+ AiTrace: aiTrace,+ }++ if aiTrace.StorageKey != "" {+ data, err := storage.Store.Read(c, aiTrace.StorageKey)+ if err == nil {+ response.Conversation = json.RawMessage(data)+ } else {+ traceway.CaptureException(traceway.NewStackTraceErrorf("failed to read AI trace conversation (key=%s): %w", aiTrace.StorageKey, err))+ }+ }++ c.JSON(http.StatusOK, response)+}++var AiTraceController = aiTraceController{}diff --git a/backend/app/controllers/otelcontrollers/helpers.go b/backend/app/controllers/otelcontrollers/helpers.goindex 6f5f713f..a52129ef 100644--- a/backend/app/controllers/otelcontrollers/helpers.go+++ b/backend/app/controllers/otelcontrollers/helpers.go@@ -97,3 +97,17 @@ func getIntAttribute(attrs []*commonpb.KeyValue, key string) (int64, bool) {}return 0, false}++func getFloatAttribute(attrs []*commonpb.KeyValue, key string) float64 {+ for _, kv := range attrs {+ if kv.Key == key && kv.Value != nil {+ switch v := kv.Value.Value.(type) {+ case *commonpb.AnyValue_DoubleValue:+ return v.DoubleValue+ case *commonpb.AnyValue_IntValue:+ return float64(v.IntValue)+ }+ }+ }+ return 0+}diff --git a/backend/app/controllers/otelcontrollers/otel.controller.go b/backend/app/controllers/otelcontrollers/otel.controller.goindex aee1ec95..59160a9f 100644--- a/backend/app/controllers/otelcontrollers/otel.controller.go+++ b/backend/app/controllers/otelcontrollers/otel.controller.go@@ -1,15 +1,20 @@package otelcontrollersimport (+ "context"+ "fmt"+ "log"+ "net/http"++ "github.com/gin-gonic/gin""github.com/tracewayapp/traceway/backend/app/hooks""github.com/tracewayapp/traceway/backend/app/middleware""github.com/tracewayapp/traceway/backend/app/models""github.com/tracewayapp/traceway/backend/app/repositories""github.com/tracewayapp/traceway/backend/app/services"- "net/http"-- "github.com/gin-gonic/gin"+ "github.com/tracewayapp/traceway/backend/app/storage"traceway "go.tracewayapp.com"+ "google.golang.org/protobuf/encoding/protojson")type otelController struct{}@@ -17,12 +22,13 @@ type otelController struct{}var OtelController = otelController{}func (o otelController) ExportTraces(c *gin.Context) {+ fmt.Println("A00")projectId, err := middleware.GetProjectId(c)if err != nil {c.AbortWithError(http.StatusInternalServerError, traceway.NewStackTraceErrorf("UseClientAuth middleware must be applied: %w", err))return}-+ fmt.Println("A0")if project, exists := c.Get(middleware.ProjectContextKey); exists {if p, ok := project.(*models.Project); ok && p.OrganizationId != nil {if !hooks.CanReport(*p.OrganizationId) {@@ -31,6 +37,7 @@ func (o otelController) ExportTraces(c *gin.Context) {}}}+ fmt.Println("A1")req, err := decodeTraceRequest(c)if err != nil {@@ -38,7 +45,12 @@ func (o otelController) ExportTraces(c *gin.Context) {return}- endpoints, tasks, spans, exceptions := convertTraces(projectId, req)+ fmt.Println("WTF??")+ if jsonBytes, err := protojson.Marshal(req); err == nil {+ log.Printf("[OTEL TRACES] Payload: %s", string(jsonBytes))+ }++ endpoints, tasks, spans, exceptions, aiTraces, aiConversations := convertTraces(projectId, req)if len(endpoints) > 0 {if err := repositories.EndpointRepository.InsertAsync(c, endpoints); err != nil {@@ -64,6 +76,24 @@ func (o otelController) ExportTraces(c *gin.Context) {return}+ if len(aiTraces) > 0 {+ if err := repositories.AiTraceRepository.InsertAsync(c, aiTraces); err != nil {+ c.AbortWithError(500, traceway.NewStackTraceErrorf("error inserting OTEL ai traces: %w", err))+ return+ }++ if len(aiConversations) > 0 {+ convs := aiConversations+ go func() {+ for _, conv := range convs {+ if err := storage.Store.Write(context.Background(), conv.StorageKey, conv.Content); err != nil {+ traceway.CaptureException(fmt.Errorf("failed to write AI trace conversation (key=%s): %w", conv.StorageKey, err))+ }+ }+ }()+ }+ }+var exceptionHashes []stringfor _, ex := range exceptions {exceptionHashes = append(exceptionHashes, ex.ExceptionHash)diff --git a/backend/app/controllers/otelcontrollers/trace_converter.go b/backend/app/controllers/otelcontrollers/trace_converter.goindex d43e6432..ff1dab08 100644--- a/backend/app/controllers/otelcontrollers/trace_converter.go+++ b/backend/app/controllers/otelcontrollers/trace_converter.go@@ -1,23 +1,31 @@package otelcontrollersimport (- "github.com/tracewayapp/traceway/backend/app/controllers/clientcontrollers"- "github.com/tracewayapp/traceway/backend/app/models"+ "encoding/json""fmt""strings""time""github.com/google/uuid"+ "github.com/tracewayapp/traceway/backend/app/controllers/clientcontrollers"+ "github.com/tracewayapp/traceway/backend/app/models"coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"commonpb "go.opentelemetry.io/proto/otlp/common/v1"tracepb "go.opentelemetry.io/proto/otlp/trace/v1")+type aiTraceConversation struct {+ StorageKey string+ Content []byte+}+func convertTraces(projectId uuid.UUID, req *coltracepb.ExportTraceServiceRequest) (endpoints []models.Endpoint,tasks []models.Task,spans []models.Span,exceptions []models.ExceptionStackTrace,+ aiTraces []models.AiTrace,+ aiConversations []aiTraceConversation,) {for _, rs := range req.ResourceSpans {@@ -80,6 +88,15 @@ func convertTraces(projectId uuid.UUID, req *coltracepb.ExportTraceServiceReques)t.DistributedTraceId = distributedTraceIdtasks = append(tasks, t)+ } else if hasGenAiAttributes(spanAttrs) {+ aiTrace := buildAiTrace(+ traceId, projectId, span, spanAttrs, allAttrs,+ startTime, duration, serverName, appVersion,+ )+ aiTraces = append(aiTraces, aiTrace)+ if conv := extractConversation(spanAttrs, projectId, traceId); conv != nil {+ aiConversations = append(aiConversations, *conv)+ }} else {continue}@@ -251,6 +268,170 @@ func buildException(}}+func hasGenAiAttributes(attrs []*commonpb.KeyValue) bool {+ for _, kv := range attrs {+ if strings.HasPrefix(kv.Key, "gen_ai.") {+ return true+ }+ }+ return false+}++func buildAiTrace(+ id, projectId uuid.UUID,+ span *tracepb.Span,+ attrs []*commonpb.KeyValue,+ allAttrs map[string]string,+ startTime time.Time,+ duration time.Duration,+ serverName, appVersion string,+) models.AiTrace {+ model := getStringAttribute(attrs, "gen_ai.request.model")+ responseModel := getStringAttribute(attrs, "gen_ai.response.model")+ provider := getStringAttribute(attrs, "gen_ai.system")+ if provider == "" {+ provider = getStringAttribute(attrs, "gen_ai.provider.name")+ }+ operation := getStringAttribute(attrs, "gen_ai.operation.name")++ inputTokens, _ := getIntAttribute(attrs, "gen_ai.usage.input_tokens")+ outputTokens, _ := getIntAttribute(attrs, "gen_ai.usage.output_tokens")+ totalTokens, hasTotalTokens := getIntAttribute(attrs, "gen_ai.usage.total_tokens")+ if !hasTotalTokens {+ totalTokens = inputTokens + outputTokens+ }+ cachedTokens, _ := getIntAttribute(attrs, "gen_ai.usage.input_tokens.cached")+ reasoningTokens, _ := getIntAttribute(attrs, "gen_ai.usage.output_tokens.reasoning")++ inputCost := getFloatAttribute(attrs, "gen_ai.usage.input_cost")+ outputCost := getFloatAttribute(attrs, "gen_ai.usage.output_cost")+ totalCost := getFloatAttribute(attrs, "gen_ai.usage.total_cost")+ if totalCost == 0 {+ totalCost = inputCost + outputCost+ }++ traceName := getStringAttribute(attrs, "trace.name")+ if traceName == "" {+ traceName = span.Name+ }++ userId := getStringAttribute(attrs, "user.id")+ finishReason := getStringAttribute(attrs, "gen_ai.response.finish_reason")+ if finishReason == "" {+ finishReason = getStringAttribute(attrs, "gen_ai.response.finish_reasons")+ }++ statusCode := uint8(span.Status.GetCode())+ storageKey := fmt.Sprintf("ai-traces/%s/%s.json", projectId, id)++ filteredAttrs := filterNonStandardAiAttrs(allAttrs)++ return models.AiTrace{+ Id: id,+ ProjectId: projectId,+ RecordedAt: startTime,+ Duration: duration,+ StatusCode: statusCode,+ Model: model,+ ResponseModel: responseModel,+ Provider: provider,+ Operation: operation,+ InputTokens: inputTokens,+ OutputTokens: outputTokens,+ TotalTokens: totalTokens,+ CachedTokens: cachedTokens,+ ReasoningTokens: reasoningTokens,+ InputCost: inputCost,+ OutputCost: outputCost,+ TotalCost: totalCost,+ TraceName: traceName,+ UserId: userId,+ FinishReason: finishReason,+ ServerName: serverName,+ AppVersion: appVersion,+ StorageKey: storageKey,+ Attributes: filteredAttrs,+ }+}++var standardAiAttrPrefixes = []string{+ "gen_ai.request.model",+ "gen_ai.response.model",+ "gen_ai.system",+ "gen_ai.provider.name",+ "gen_ai.operation.name",+ "gen_ai.usage.",+ "gen_ai.prompt",+ "gen_ai.completion",+ "gen_ai.response.finish_reason",+ "gen_ai.response.finish_reasons",+ "trace.name",+ "trace.input",+ "trace.output",+ "span.input",+ "span.output",+ "user.id",+}++func filterNonStandardAiAttrs(allAttrs map[string]string) map[string]string {+ if len(allAttrs) == 0 {+ return nil+ }+ result := make(map[string]string)+ for k, v := range allAttrs {+ standard := false+ for _, prefix := range standardAiAttrPrefixes {+ if k == prefix || strings.HasPrefix(k, prefix) {+ standard = true+ break+ }+ }+ if !standard {+ result[k] = v+ }+ }+ if len(result) == 0 {+ return nil+ }+ return result+}++func extractConversation(attrs []*commonpb.KeyValue, projectId, traceId uuid.UUID) *aiTraceConversation {+ input := getStringAttribute(attrs, "gen_ai.prompt")+ if input == "" {+ input = getStringAttribute(attrs, "trace.input")+ }+ if input == "" {+ input = getStringAttribute(attrs, "span.input")+ }++ output := getStringAttribute(attrs, "gen_ai.completion")+ if output == "" {+ output = getStringAttribute(attrs, "trace.output")+ }+ if output == "" {+ output = getStringAttribute(attrs, "span.output")+ }++ if input == "" && output == "" {+ return nil+ }++ content := map[string]string{+ "input": input,+ "output": output,+ }+ data, err := json.Marshal(content)+ if err != nil {+ return nil+ }++ return &aiTraceConversation{+ StorageKey: fmt.Sprintf("ai-traces/%s/%s.json", projectId, traceId),+ Content: data,+ }+}+func formatExceptionStackTrace(excType, excMessage, excStacktrace string) string {header := excTypeif excMessage != "" {diff --git a/backend/app/controllers/routes.go b/backend/app/controllers/routes.goindex 4a974fbe..b1aec01c 100644--- a/backend/app/controllers/routes.go+++ b/backend/app/controllers/routes.go@@ -85,6 +85,11 @@ func RegisterControllers(router *gin.RouterGroup) {router.POST("/tasks/task", middleware.UseAppAuth, middleware.RequireProjectAccess, TaskController.FindByTaskName)router.POST("/tasks/:taskId", middleware.UseAppAuth, middleware.RequireProjectAccess, TaskDetailController.GetTaskDetail)+ // AI Traces (projectId in body)+ router.POST("/ai-traces/grouped", middleware.UseAppAuth, middleware.RequireProjectAccess, AiTraceController.FindGroupedByTraceName)+ router.POST("/ai-traces/trace", middleware.UseAppAuth, middleware.RequireProjectAccess, AiTraceController.FindByTraceName)+ router.POST("/ai-traces/:traceId", middleware.UseAppAuth, middleware.RequireProjectAccess, AiTraceController.GetAiTraceDetail)+// Distributed tracesrouter.POST("/distributed-traces/:distributedTraceId", middleware.UseAppAuth, DistributedTraceController.GetDistributedTrace)diff --git a/backend/app/migrations/ch/0044_create_ai_traces.up.sql b/backend/app/migrations/ch/0044_create_ai_traces.up.sqlnew file mode 100644index 00000000..a3f3b5c1--- /dev/null+++ b/backend/app/migrations/ch/0044_create_ai_traces.up.sql@@ -0,0 +1,34 @@+CREATE TABLE IF NOT EXISTS ai_traces+(+ `id` UUID,+ `project_id` UUID,+ `recorded_at` DateTime64(3),+ `duration` Int64,+ `status_code` UInt8,+ `model` LowCardinality(String) DEFAULT '',+ `response_model` LowCardinality(String) DEFAULT '',+ `provider` LowCardinality(String) DEFAULT '',+ `operation` LowCardinality(String) DEFAULT '',+ `input_tokens` Int64 DEFAULT 0,+ `output_tokens` Int64 DEFAULT 0,+ `total_tokens` Int64 DEFAULT 0,+ `cached_tokens` Int64 DEFAULT 0,+ `reasoning_tokens` Int64 DEFAULT 0,+ `input_cost` Float64 DEFAULT 0,+ `output_cost` Float64 DEFAULT 0,+ `total_cost` Float64 DEFAULT 0,+ `trace_name` LowCardinality(String) DEFAULT '',+ `user_id` String DEFAULT '',+ `finish_reason` LowCardinality(String) DEFAULT '',+ `server_name` LowCardinality(String) DEFAULT '',+ `app_version` LowCardinality(String) DEFAULT '',+ `storage_key` String DEFAULT '',+ `attributes` String DEFAULT '{}',+ INDEX idx_id id TYPE bloom_filter(0.001) GRANULARITY 1,+ INDEX idx_trace_name trace_name TYPE set(100) GRANULARITY 4,+ INDEX idx_model model TYPE set(100) GRANULARITY 4+)+ENGINE = MergeTree+PARTITION BY toYYYYMMDD(recorded_at)+ORDER BY (project_id, recorded_at, trace_name)+SETTINGS index_granularity = 8192diff --git a/backend/app/migrations/sqlite_telemetry/0002_create_ai_traces.up.sql b/backend/app/migrations/sqlite_telemetry/0002_create_ai_traces.up.sqlnew file mode 100644index 00000000..c19b3a82--- /dev/null+++ b/backend/app/migrations/sqlite_telemetry/0002_create_ai_traces.up.sql@@ -0,0 +1,28 @@+CREATE TABLE IF NOT EXISTS ai_traces (+ id TEXT NOT NULL,+ project_id TEXT NOT NULL,+ recorded_at DATETIME NOT NULL,+ duration INTEGER NOT NULL DEFAULT 0,+ status_code INTEGER NOT NULL DEFAULT 0,+ model TEXT NOT NULL DEFAULT '',+ response_model TEXT NOT NULL DEFAULT '',+ provider TEXT NOT NULL DEFAULT '',+ operation TEXT NOT NULL DEFAULT '',+ input_tokens INTEGER NOT NULL DEFAULT 0,+ output_tokens INTEGER NOT NULL DEFAULT 0,+ total_tokens INTEGER NOT NULL DEFAULT 0,+ cached_tokens INTEGER NOT NULL DEFAULT 0,+ reasoning_tokens INTEGER NOT NULL DEFAULT 0,+ input_cost REAL NOT NULL DEFAULT 0,+ output_cost REAL NOT NULL DEFAULT 0,+ total_cost REAL NOT NULL DEFAULT 0,+ trace_name TEXT NOT NULL DEFAULT '',+ user_id TEXT NOT NULL DEFAULT '',+ finish_reason TEXT NOT NULL DEFAULT '',+ server_name TEXT NOT NULL DEFAULT '',+ app_version TEXT NOT NULL DEFAULT '',+ storage_key TEXT NOT NULL DEFAULT '',+ attributes TEXT NOT NULL DEFAULT '{}'+);+CREATE INDEX IF NOT EXISTS idx_ai_traces_project_recorded ON ai_traces(project_id, recorded_at);+CREATE INDEX IF NOT EXISTS idx_ai_traces_project_trace_name ON ai_traces(project_id, trace_name);diff --git a/backend/app/models/ai_trace.model.go b/backend/app/models/ai_trace.model.gonew file mode 100644index 00000000..2edeb40e--- /dev/null+++ b/backend/app/models/ai_trace.model.go@@ -0,0 +1,59 @@+package models++import (+ "time"++ "github.com/google/uuid"+)++type AiTrace struct {+ Id uuid.UUID `json:"id" ch:"id"`+ ProjectId uuid.UUID `json:"projectId" ch:"project_id"`+ RecordedAt time.Time `json:"recordedAt" ch:"recorded_at"`+ Duration time.Duration `json:"duration" ch:"duration"`+ StatusCode uint8 `json:"statusCode" ch:"status_code"`+ Model string `json:"model" ch:"model"`+ ResponseModel string `json:"responseModel" ch:"response_model"`+ Provider string `json:"provider" ch:"provider"`+ Operation string `json:"operation" ch:"operation"`+ InputTokens int64 `json:"inputTokens" ch:"input_tokens"`+ OutputTokens int64 `json:"outputTokens" ch:"output_tokens"`+ TotalTokens int64 `json:"totalTokens" ch:"total_tokens"`+ CachedTokens int64 `json:"cachedTokens" ch:"cached_tokens"`+ ReasoningTokens int64 `json:"reasoningTokens" ch:"reasoning_tokens"`+ InputCost float64 `json:"inputCost" ch:"input_cost"`+ OutputCost float64 `json:"outputCost" ch:"output_cost"`+ TotalCost float64 `json:"totalCost" ch:"total_cost"`+ TraceName string `json:"traceName" ch:"trace_name"`+ UserId string `json:"userId" ch:"user_id"`+ FinishReason string `json:"finishReason" ch:"finish_reason"`+ ServerName string `json:"serverName" ch:"server_name"`+ AppVersion string `json:"appVersion" ch:"app_version"`+ StorageKey string `json:"storageKey" ch:"storage_key"`+ Attributes map[string]string `json:"attributes" ch:"attributes"`+}++type AiTraceStats struct {+ TraceName string `json:"traceName"`+ Count uint64 `json:"count"`+ P50Duration time.Duration `json:"p50Duration"`+ P95Duration time.Duration `json:"p95Duration"`+ AvgDuration time.Duration `json:"avgDuration"`+ TotalTokens int64 `json:"totalTokens"`+ TotalCost float64 `json:"totalCost"`+ AvgInputTokens float64 `json:"avgInputTokens"`+ AvgOutputTokens float64 `json:"avgOutputTokens"`+ LastSeen time.Time `json:"lastSeen"`+}++type AiTraceDetailStats struct {+ Count int64 `json:"count"`+ AvgDuration float64 `json:"avgDuration"`+ MedianDuration float64 `json:"medianDuration"`+ P95Duration float64 `json:"p95Duration"`+ TotalTokens int64 `json:"totalTokens"`+ TotalCost float64 `json:"totalCost"`+ AvgInputTokens float64 `json:"avgInputTokens"`+ AvgOutputTokens float64 `json:"avgOutputTokens"`+ Throughput float64 `json:"throughput"`+}diff --git a/backend/app/repositories/ai_trace.repository.go b/backend/app/repositories/ai_trace.repository.gonew file mode 100644index 00000000..edc1341d--- /dev/null+++ b/backend/app/repositories/ai_trace.repository.go@@ -0,0 +1,271 @@+//go:build pgch++package repositories++import (+ "context"+ "database/sql"+ "encoding/json"+ "errors"+ "time"++ "github.com/ClickHouse/clickhouse-go/v2"+ "github.com/google/uuid"+ "github.com/tracewayapp/traceway/backend/app/chdb"+ "github.com/tracewayapp/traceway/backend/app/models"+)++type aiTraceRepository struct{}++func (r *aiTraceRepository) InsertAsync(ctx context.Context, lines []models.AiTrace) error {+ batch, err := chdb.Conn.PrepareBatch(clickhouse.Context(context.Background(), clickhouse.WithAsync(false)),+ "INSERT INTO ai_traces (id, project_id, recorded_at, duration, status_code, model, response_model, provider, operation, input_tokens, output_tokens, total_tokens, cached_tokens, reasoning_tokens, input_cost, output_cost, total_cost, trace_name, user_id, finish_reason, server_name, app_version, storage_key, attributes)")+ if err != nil {+ return err+ }+ for _, t := range lines {+ attributesJSON := "{}"+ if len(t.Attributes) != 0 {+ if attributesBytes, err := json.Marshal(t.Attributes); err == nil {+ attributesJSON = string(attributesBytes)+ }+ }+ if err := batch.Append(+ t.Id, t.ProjectId, t.RecordedAt, int64(t.Duration), t.StatusCode,+ t.Model, t.ResponseModel, t.Provider, t.Operation,+ t.InputTokens, t.OutputTokens, t.TotalTokens, t.CachedTokens, t.ReasoningTokens,+ t.InputCost, t.OutputCost, t.TotalCost,+ t.TraceName, t.UserId, t.FinishReason, t.ServerName, t.AppVersion,+ t.StorageKey, attributesJSON,+ ); err != nil {+ return err+ }+ }+ return batch.Send()+}++func (r *aiTraceRepository) FindGroupedByTraceName(ctx context.Context, projectId uuid.UUID, fromDate, toDate time.Time, page, pageSize int, orderBy, sortDirection, search string) ([]models.AiTraceStats, int64, error) {+ whereClause := "project_id = ? AND recorded_at >= ? AND recorded_at <= ?"+ args := []interface{}{projectId, fromDate, toDate}++ if search != "" {+ whereClause += " AND positionCaseInsensitive(trace_name, ?) > 0"+ args = append(args, search)+ }++ var count uint64+ err := chdb.Conn.QueryRow(ctx, "SELECT uniq(trace_name) FROM ai_traces WHERE "+whereClause, args...).Scan(&count)+ if err != nil {+ return nil, 0, err+ }++ offset := (page - 1) * pageSize++ orderByMap := map[string]string{+ "count": "count",+ "p50_duration": "p50_duration",+ "p95_duration": "p95_duration",+ "avg_duration": "avg_duration",+ "total_tokens": "total_tokens",+ "total_cost": "total_cost",+ "last_seen": "last_seen",+ }+ orderExpr, ok := orderByMap[orderBy]+ if !ok {+ orderExpr = "total_cost"+ }++ sortDir := "DESC"+ if sortDirection == "asc" {+ sortDir = "ASC"+ }++ query := `SELECT+ trace_name,+ count() as count,+ quantile(0.5)(duration) as p50_duration,+ quantile(0.95)(duration) as p95_duration,+ avg(duration) as avg_duration,+ sum(total_tokens) as total_tokens,+ sum(total_cost) as total_cost,+ avg(input_tokens) as avg_input_tokens,+ avg(output_tokens) as avg_output_tokens,+ max(recorded_at) as last_seen+ FROM ai_traces+ WHERE ` + whereClause + `+ GROUP BY trace_name+ ORDER BY ` + orderExpr + ` ` + sortDir + `+ LIMIT ? OFFSET ?`++ queryArgs := append(args, pageSize, offset)+ rows, err := chdb.Conn.Query(ctx, query, queryArgs...)+ if err != nil {+ return nil, 0, err+ }+ defer rows.Close()++ var stats []models.AiTraceStats+ for rows.Next() {+ var s models.AiTraceStats+ var p50, p95, avg float64+ if err := rows.Scan(&s.TraceName, &s.Count, &p50, &p95, &avg,+ &s.TotalTokens, &s.TotalCost, &s.AvgInputTokens, &s.AvgOutputTokens, &s.LastSeen); err != nil {+ return nil, 0, err+ }+ s.P50Duration = time.Duration(p50)+ s.P95Duration = time.Duration(p95)+ s.AvgDuration = time.Duration(avg)+ stats = append(stats, s)+ }++ return stats, int64(count), nil+}++func (r *aiTraceRepository) FindByTraceName(ctx context.Context, projectId uuid.UUID, traceName string, fromDate, toDate time.Time, page, pageSize int, orderBy, sortDirection string) ([]models.AiTrace, int64, error) {+ var count uint64+ err := chdb.Conn.QueryRow(ctx, "SELECT count() FROM ai_traces WHERE project_id = ? AND trace_name = ? AND recorded_at >= ? AND recorded_at <= ?", projectId, traceName, fromDate, toDate).Scan(&count)+ if err != nil {+ return nil, 0, err+ }++ offset := (page - 1) * pageSize++ allowedOrderBy := map[string]bool{+ "recorded_at": true,+ "duration": true,+ "total_tokens": true,+ "total_cost": true,+ "input_tokens": true,+ "output_tokens": true,+ }+ if !allowedOrderBy[orderBy] {+ orderBy = "recorded_at"+ }++ sortDir := "DESC"+ if sortDirection == "asc" {+ sortDir = "ASC"+ }++ query := `SELECT id, project_id, recorded_at, duration, status_code,+ model, response_model, provider, operation,+ input_tokens, output_tokens, total_tokens, cached_tokens, reasoning_tokens,+ input_cost, output_cost, total_cost,+ trace_name, user_id, finish_reason, server_name, app_version,+ storage_key, attributes+ FROM ai_traces+ WHERE project_id = ? AND trace_name = ? AND recorded_at >= ? AND recorded_at <= ?+ ORDER BY ` + orderBy + ` ` + sortDir + `+ LIMIT ? OFFSET ?`++ rows, err := chdb.Conn.Query(ctx, query, projectId, traceName, fromDate, toDate, pageSize, offset)+ if err != nil {+ return nil, 0, err+ }+ defer rows.Close()++ var traces []models.AiTrace+ for rows.Next() {+ var t models.AiTrace+ var attributesJSON string+ if err := rows.Scan(+ &t.Id, &t.ProjectId, &t.RecordedAt, &t.Duration, &t.StatusCode,+ &t.Model, &t.ResponseModel, &t.Provider, &t.Operation,+ &t.InputTokens, &t.OutputTokens, &t.TotalTokens, &t.CachedTokens, &t.ReasoningTokens,+ &t.InputCost, &t.OutputCost, &t.TotalCost,+ &t.TraceName, &t.UserId, &t.FinishReason, &t.ServerName, &t.AppVersion,+ &t.StorageKey, &attributesJSON,+ ); err != nil {+ return nil, 0, err+ }+ if attributesJSON != "" && attributesJSON != "{}" {+ if err := json.Unmarshal([]byte(attributesJSON), &t.Attributes); err != nil {+ t.Attributes = nil+ }+ }+ traces = append(traces, t)+ }++ return traces, int64(count), nil+}++func (r *aiTraceRepository) GetTraceNameStats(ctx context.Context, projectId uuid.UUID, traceName string, start, end time.Time) (*models.AiTraceDetailStats, error) {+ durationMinutes := end.Sub(start).Minutes()+ if durationMinutes < 1 {+ durationMinutes = 1+ }++ query := `SELECT+ count() as count,+ if(count() > 0, avg(duration) / 1000000, 0) as avg_duration_ms,+ if(count() > 0, quantile(0.5)(duration) / 1000000, 0) as median_duration_ms,+ if(count() > 0, quantile(0.95)(duration) / 1000000, 0) as p95_duration_ms,+ sum(total_tokens) as total_tokens,+ sum(total_cost) as total_cost,+ avg(input_tokens) as avg_input_tokens,+ avg(output_tokens) as avg_output_tokens+ FROM ai_traces+ WHERE project_id = ? AND trace_name = ? AND recorded_at >= ? AND recorded_at <= ?`++ var stats models.AiTraceDetailStats+ var count uint64++ err := chdb.Conn.QueryRow(ctx, query, projectId, traceName, start, end).Scan(+ &count,+ &stats.AvgDuration,+ &stats.MedianDuration,+ &stats.P95Duration,+ &stats.TotalTokens,+ &stats.TotalCost,+ &stats.AvgInputTokens,+ &stats.AvgOutputTokens,+ )+ if err != nil {+ return nil, err+ }++ stats.Count = int64(count)+ stats.Throughput = float64(count) / durationMinutes++ return &stats, nil+}++func (r *aiTraceRepository) FindById(ctx context.Context, projectId, traceId uuid.UUID) (*models.AiTrace, error) {+ query := `SELECT id, project_id, recorded_at, duration, status_code,+ model, response_model, provider, operation,+ input_tokens, output_tokens, total_tokens, cached_tokens, reasoning_tokens,+ input_cost, output_cost, total_cost,+ trace_name, user_id, finish_reason, server_name, app_version,+ storage_key, attributes+ FROM ai_traces+ WHERE project_id = ? AND id = ?+ LIMIT 1`++ var t models.AiTrace+ var attributesJSON string++ err := chdb.Conn.QueryRow(ctx, query, projectId, traceId).Scan(+ &t.Id, &t.ProjectId, &t.RecordedAt, &t.Duration, &t.StatusCode,+ &t.Model, &t.ResponseModel, &t.Provider, &t.Operation,+ &t.InputTokens, &t.OutputTokens, &t.TotalTokens, &t.CachedTokens, &t.ReasoningTokens,+ &t.InputCost, &t.OutputCost, &t.TotalCost,+ &t.TraceName, &t.UserId, &t.FinishReason, &t.ServerName, &t.AppVersion,+ &t.StorageKey, &attributesJSON,+ )+ if err != nil {+ if errors.Is(err, sql.ErrNoRows) {+ return nil, nil+ }+ return nil, err+ }++ if attributesJSON != "" && attributesJSON != "{}" {+ if err := json.Unmarshal([]byte(attributesJSON), &t.Attributes); err != nil {+ t.Attributes = nil+ }+ }++ return &t, nil+}++var AiTraceRepository = aiTraceRepository{}diff --git a/backend/app/repositories/ai_trace.repository_sqlite.go b/backend/app/repositories/ai_trace.repository_sqlite.gonew file mode 100644index 00000000..70677df6--- /dev/null+++ b/backend/app/repositories/ai_trace.repository_sqlite.go@@ -0,0 +1,380 @@+//go:build !pgch++package repositories++import (+ "context"+ "fmt"+ "sort"+ "time"++ "github.com/google/uuid"+ "github.com/tracewayapp/lit/v2"+ "github.com/tracewayapp/traceway/backend/app/db"+ "github.com/tracewayapp/traceway/backend/app/models"+)++type aiTraceRow struct {+ Id uuid.UUID `lit:"id"`+ ProjectId uuid.UUID `lit:"project_id"`+ RecordedAt SQLiteTime `lit:"recorded_at"`+ Duration int64 `lit:"duration"`+ StatusCode uint8 `lit:"status_code"`+ Model string `lit:"model"`+ ResponseModel string `lit:"response_model"`+ Provider string `lit:"provider"`+ Operation string `lit:"operation"`+ InputTokens int64 `lit:"input_tokens"`+ OutputTokens int64 `lit:"output_tokens"`+ TotalTokens int64 `lit:"total_tokens"`+ CachedTokens int64 `lit:"cached_tokens"`+ ReasoningTokens int64 `lit:"reasoning_tokens"`+ InputCost float64 `lit:"input_cost"`+ OutputCost float64 `lit:"output_cost"`+ TotalCost float64 `lit:"total_cost"`+ TraceName string `lit:"trace_name"`+ UserId string `lit:"user_id"`+ FinishReason string `lit:"finish_reason"`+ ServerName string `lit:"server_name"`+ AppVersion string `lit:"app_version"`+ StorageKey string `lit:"storage_key"`+ Attributes SQLiteJSONMap `lit:"attributes"`+}++type groupedAiTraceRow struct {+ TraceName string `lit:"trace_name"`+ TotalCount uint64 `lit:"total_count"`+ AvgDuration float64 `lit:"avg_duration"`+ TotalTokens int64 `lit:"total_tokens"`+ TotalCost float64 `lit:"total_cost"`+ AvgInputTokens float64 `lit:"avg_input_tokens"`+ AvgOutputTokens float64 `lit:"avg_output_tokens"`+ LastSeen string `lit:"last_seen"`+}++type aiTraceDurationRow struct {+ Duration float64 `lit:"duration"`+}++type aiTraceDetailStatsRow struct {+ Count int64 `lit:"count"`+ AvgDurationMs float64 `lit:"avg_duration_ms"`+ TotalTokens int64 `lit:"total_tokens"`+ TotalCost float64 `lit:"total_cost"`+ AvgInputTokens float64 `lit:"avg_input_tokens"`+ AvgOutputTokens float64 `lit:"avg_output_tokens"`+}++func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[aiTraceRow](driver)+ lit.RegisterModel[groupedAiTraceRow](driver)+ lit.RegisterModel[aiTraceDurationRow](driver)+ lit.RegisterModel[aiTraceDetailStatsRow](driver)+ })+}++func aiTraceToRow(t models.AiTrace) aiTraceRow {+ return aiTraceRow{+ Id: t.Id,+ ProjectId: t.ProjectId,+ RecordedAt: NewSQLiteTime(t.RecordedAt),+ Duration: int64(t.Duration),+ StatusCode: t.StatusCode,+ Model: t.Model,+ ResponseModel: t.ResponseModel,+ Provider: t.Provider,+ Operation: t.Operation,+ InputTokens: t.InputTokens,+ OutputTokens: t.OutputTokens,+ TotalTokens: t.TotalTokens,+ CachedTokens: t.CachedTokens,+ ReasoningTokens: t.ReasoningTokens,+ InputCost: t.InputCost,+ OutputCost: t.OutputCost,+ TotalCost: t.TotalCost,+ TraceName: t.TraceName,+ UserId: t.UserId,+ FinishReason: t.FinishReason,+ ServerName: t.ServerName,+ AppVersion: t.AppVersion,+ StorageKey: t.StorageKey,+ Attributes: NewSQLiteJSONMap(t.Attributes),+ }+}++func (r *aiTraceRow) toModel() models.AiTrace {+ t := models.AiTrace{+ Id: r.Id,+ ProjectId: r.ProjectId,+ RecordedAt: r.RecordedAt.Time,+ Duration: time.Duration(r.Duration),+ StatusCode: r.StatusCode,+ Model: r.Model,+ ResponseModel: r.ResponseModel,+ Provider: r.Provider,+ Operation: r.Operation,+ InputTokens: r.InputTokens,+ OutputTokens: r.OutputTokens,+ TotalTokens: r.TotalTokens,+ CachedTokens: r.CachedTokens,+ ReasoningTokens: r.ReasoningTokens,+ InputCost: r.InputCost,+ OutputCost: r.OutputCost,+ TotalCost: r.TotalCost,+ TraceName: r.TraceName,+ UserId: r.UserId,+ FinishReason: r.FinishReason,+ ServerName: r.ServerName,+ AppVersion: r.AppVersion,+ StorageKey: r.StorageKey,+ }+ if r.Attributes != nil {+ t.Attributes = map[string]string(r.Attributes)+ }+ return t+}++type aiTraceRepository struct{}++func (r *aiTraceRepository) InsertAsync(ctx context.Context, lines []models.AiTrace) error {+ if len(lines) == 0 {+ return nil+ }+ for _, t := range lines {+ row := aiTraceToRow(t)+ if err := lit.InsertExistingUuid(db.TelemetryDB, &row); err != nil {+ return err+ }+ }+ return nil+}++func (r *aiTraceRepository) FindGroupedByTraceName(ctx context.Context, projectId uuid.UUID, fromDate, toDate time.Time, page, pageSize int, orderBy, sortDirection, search string) ([]models.AiTraceStats, int64, error) {+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}++ whereClause := "project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to"+ if search != "" {+ whereClause += " AND INSTR(LOWER(trace_name), LOWER(:search)) > 0"+ params["search"] = search+ }++ countResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(DISTINCT trace_name) AS count FROM ai_traces WHERE "+whereClause, params)+ if err != nil {+ return nil, 0, err+ }+ count := int64(0)+ if countResult != nil {+ count = int64(countResult.Count)+ }++ rows, err := lit.SelectNamed[groupedAiTraceRow](db.TelemetryDB,+ `SELECT trace_name, COUNT(*) AS total_count,+ AVG(duration) AS avg_duration,+ SUM(total_tokens) AS total_tokens,+ SUM(total_cost) AS total_cost,+ AVG(input_tokens) AS avg_input_tokens,+ AVG(output_tokens) AS avg_output_tokens,+ MAX(recorded_at) AS last_seen+ FROM ai_traces WHERE `+whereClause+`+ GROUP BY trace_name`, params)+ if err != nil {+ return nil, 0, err+ }++ var stats []models.AiTraceStats+ for _, row := range rows {+ // Compute percentiles from raw durations for this trace_name+ durationParams := lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate), "trace_name": row.TraceName}+ durationRows, err := lit.SelectNamed[aiTraceDurationRow](db.TelemetryDB,+ `SELECT CAST(duration AS REAL) AS duration FROM ai_traces+ WHERE project_id = :project_id AND trace_name = :trace_name AND recorded_at >= :from AND recorded_at <= :to+ ORDER BY duration ASC`, durationParams)+ if err != nil {+ return nil, 0, err+ }++ sortedDurations := make([]float64, len(durationRows))+ for i, d := range durationRows {+ sortedDurations[i] = d.Duration+ }++ lastSeen, _ := time.Parse("2006-01-02 15:04:05", row.LastSeen)++ stats = append(stats, models.AiTraceStats{+ TraceName: row.TraceName,+ Count: row.TotalCount,+ P50Duration: time.Duration(computePercentile(sortedDurations, 0.5)),+ P95Duration: time.Duration(computePercentile(sortedDurations, 0.95)),+ AvgDuration: time.Duration(row.AvgDuration),+ TotalTokens: row.TotalTokens,+ TotalCost: row.TotalCost,+ AvgInputTokens: row.AvgInputTokens,+ AvgOutputTokens: row.AvgOutputTokens,+ LastSeen: lastSeen,+ })+ }++ // Sort results+ orderByMap := map[string]func(i, j int) bool{+ "count": func(i, j int) bool { return stats[i].Count > stats[j].Count },+ "p50_duration": func(i, j int) bool { return stats[i].P50Duration > stats[j].P50Duration },+ "p95_duration": func(i, j int) bool { return stats[i].P95Duration > stats[j].P95Duration },+ "avg_duration": func(i, j int) bool { return stats[i].AvgDuration > stats[j].AvgDuration },+ "total_tokens": func(i, j int) bool { return stats[i].TotalTokens > stats[j].TotalTokens },+ "total_cost": func(i, j int) bool { return stats[i].TotalCost > stats[j].TotalCost },+ "last_seen": func(i, j int) bool { return stats[i].LastSeen.After(stats[j].LastSeen) },+ }++ sortFn, ok := orderByMap[orderBy]+ if !ok {+ sortFn = orderByMap["total_cost"]+ }++ if sortDirection == "asc" {+ origFn := sortFn+ sortFn = func(i, j int) bool { return !origFn(i, j) }+ }+ sort.Slice(stats, sortFn)++ // Paginate+ offset := (page - 1) * pageSize+ end := offset + pageSize+ if offset > len(stats) {+ stats = nil+ } else if end > len(stats) {+ stats = stats[offset:]+ } else {+ stats = stats[offset:end]+ }++ return stats, count, nil+}++func (r *aiTraceRepository) FindByTraceName(ctx context.Context, projectId uuid.UUID, traceName string, fromDate, toDate time.Time, page, pageSize int, orderBy, sortDirection string) ([]models.AiTrace, int64, error) {+ params := lit.P{"project_id": projectId, "trace_name": traceName, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}++ countResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM ai_traces WHERE project_id = :project_id AND trace_name = :trace_name AND recorded_at >= :from AND recorded_at <= :to",+ params)+ if err != nil {+ return nil, 0, err+ }+ count := int64(0)+ if countResult != nil {+ count = int64(countResult.Count)+ }++ offset := (page - 1) * pageSize++ allowedOrderBy := map[string]bool{+ "recorded_at": true, "duration": true, "total_tokens": true,+ "total_cost": true, "input_tokens": true, "output_tokens": true,+ }+ if !allowedOrderBy[orderBy] {+ orderBy = "recorded_at"+ }+ sortDir := "DESC"+ if sortDirection == "asc" {+ sortDir = "ASC"+ }++ rows, err := lit.SelectNamed[aiTraceRow](db.TelemetryDB,+ fmt.Sprintf(`SELECT id, project_id, recorded_at, duration, status_code,+ model, response_model, provider, operation,+ input_tokens, output_tokens, total_tokens, cached_tokens, reasoning_tokens,+ input_cost, output_cost, total_cost,+ trace_name, user_id, finish_reason, server_name, app_version,+ storage_key, attributes+ FROM ai_traces+ WHERE project_id = :project_id AND trace_name = :trace_name AND recorded_at >= :from AND recorded_at <= :to+ ORDER BY %s %s LIMIT :limit OFFSET :offset`, orderBy, sortDir),+ lit.P{"project_id": projectId, "trace_name": traceName, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate), "limit": pageSize, "offset": offset})+ if err != nil {+ return nil, 0, err+ }++ traces := make([]models.AiTrace, 0, len(rows))+ for _, row := range rows {+ traces = append(traces, row.toModel())+ }++ return traces, count, nil+}++func (r *aiTraceRepository) GetTraceNameStats(ctx context.Context, projectId uuid.UUID, traceName string, start, end time.Time) (*models.AiTraceDetailStats, error) {+ durationMinutes := end.Sub(start).Minutes()+ if durationMinutes < 1 {+ durationMinutes = 1+ }++ params := lit.P{"project_id": projectId, "trace_name": traceName, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)}++ row, err := lit.SelectSingleNamed[aiTraceDetailStatsRow](db.TelemetryDB,+ `SELECT COUNT(*) AS count,+ CASE WHEN COUNT(*) > 0 THEN AVG(duration) / 1000000.0 ELSE 0 END AS avg_duration_ms,+ SUM(total_tokens) AS total_tokens,+ SUM(total_cost) AS total_cost,+ AVG(input_tokens) AS avg_input_tokens,+ AVG(output_tokens) AS avg_output_tokens+ FROM ai_traces+ WHERE project_id = :project_id AND trace_name = :trace_name AND recorded_at >= :from AND recorded_at <= :to`,+ params)+ if err != nil {+ return nil, err+ }++ stats := &models.AiTraceDetailStats{}+ if row != nil {+ stats.Count = row.Count+ stats.AvgDuration = row.AvgDurationMs+ stats.TotalTokens = row.TotalTokens+ stats.TotalCost = row.TotalCost+ stats.AvgInputTokens = row.AvgInputTokens+ stats.AvgOutputTokens = row.AvgOutputTokens+ stats.Throughput = float64(row.Count) / durationMinutes+ }++ // Compute median and p95 from raw durations+ durationRows, err := lit.SelectNamed[aiTraceDurationRow](db.TelemetryDB,+ `SELECT CAST(duration AS REAL) / 1000000.0 AS duration FROM ai_traces+ WHERE project_id = :project_id AND trace_name = :trace_name AND recorded_at >= :from AND recorded_at <= :to+ ORDER BY duration ASC`, params)+ if err != nil {+ return stats, nil+ }++ sortedDurations := make([]float64, len(durationRows))+ for i, d := range durationRows {+ sortedDurations[i] = d.Duration+ }+ stats.MedianDuration = computePercentile(sortedDurations, 0.5)+ stats.P95Duration = computePercentile(sortedDurations, 0.95)++ return stats, nil+}++func (r *aiTraceRepository) FindById(ctx context.Context, projectId, traceId uuid.UUID) (*models.AiTrace, error) {+ row, err := lit.SelectSingleNamed[aiTraceRow](db.TelemetryDB,+ `SELECT id, project_id, recorded_at, duration, status_code,+ model, response_model, provider, operation,+ input_tokens, output_tokens, total_tokens, cached_tokens, reasoning_tokens,+ input_cost, output_cost, total_cost,+ trace_name, user_id, finish_reason, server_name, app_version,+ storage_key, attributes+ FROM ai_traces+ WHERE project_id = :project_id AND id = :id+ LIMIT 1`,+ lit.P{"project_id": projectId, "id": traceId})+ if err != nil {+ return nil, err+ }+ if row == nil {+ return nil, nil+ }+ result := row.toModel()+ return &result, nil+}++var AiTraceRepository = aiTraceRepository{}diff --git a/docs/components/FrameworkPicker.jsx b/docs/components/FrameworkPicker.jsxindex b1e346ea..4a60e0f7 100644--- a/docs/components/FrameworkPicker.jsx+++ b/docs/components/FrameworkPicker.jsx@@ -2,6 +2,14 @@ import { useRouter } from "next/router";import { useSdk } from "./SdkContext";const FRAMEWORKS = [+ {+ value: "openrouter",+ label: "OpenRouter",+ description: "AI observability for OpenRouter with automatic OTLP trace export.",+ icon: "/openrouter.png",+ href: "/client/openrouter",+ badge: "new",+ },{value: "otel",label: "OpenTelemetry (otel)",diff --git a/docs/components/SdkContext.jsx b/docs/components/SdkContext.jsxindex 06408607..0dfeffab 100644--- a/docs/components/SdkContext.jsx+++ b/docs/components/SdkContext.jsx@@ -15,6 +15,7 @@ export const SDK_OPTIONS = [{ value: 'js-svelte', label: 'Svelte' },{ value: 'js-jquery', label: 'jQuery' },{ value: 'js-generic', label: 'JS Generic' },+ { value: 'openrouter', label: 'OpenRouter' },{ value: 'otel', label: 'OpenTelemetry (otel)' },{ value: 'cloudflare', label: 'Cloudflare Workers' },{ value: 'php-symfony', label: 'Symfony' },@@ -35,6 +36,7 @@ const PATH_SDK_MAP = {'vue': 'js-vue','svelte': 'js-svelte','jquery': 'js-jquery',+ 'openrouter': 'openrouter','otel': 'otel','cloudflare': 'cloudflare','symfony': 'php-symfony',diff --git a/docs/pages/client/_meta.json b/docs/pages/client/_meta.jsonindex 44413ce8..5bb3bb6a 100644--- a/docs/pages/client/_meta.json+++ b/docs/pages/client/_meta.json@@ -18,6 +18,7 @@"svelte": "Svelte","jquery": "jQuery","js-sdk": "JS SDK Reference",+ "openrouter": "OpenRouter","otel": "OpenTelemetry","cloudflare": "Cloudflare Workers","symfony": "Symfony"diff --git a/docs/pages/client/openrouter/_meta.json b/docs/pages/client/openrouter/_meta.jsonnew file mode 100644index 00000000..d78423de--- /dev/null+++ b/docs/pages/client/openrouter/_meta.json@@ -0,0 +1,3 @@+{+ "index": "Setup Guide"+}diff --git a/docs/pages/client/openrouter/index.mdx b/docs/pages/client/openrouter/index.mdxnew file mode 100644index 00000000..ea10a35b--- /dev/null+++ b/docs/pages/client/openrouter/index.mdx@@ -0,0 +1,96 @@+# OpenRouter Integration++Send AI traces from [OpenRouter](https://openrouter.ai) to Traceway with zero code changes. OpenRouter's built-in observability feature exports OTLP traces for every LLM call, giving you full visibility into costs, token usage, latency, and conversations.++## How It Works++1. **OpenRouter routes your AI calls** to the best model provider (OpenAI, Anthropic, Google, etc.)+2. **Observability broadcasts traces** — OpenRouter sends an OTLP span for each completion with `gen_ai.*` attributes+3. **Traceway captures and stores** — Model, tokens, costs, duration, and conversation content appear in your AI Traces dashboard++No SDK or code changes needed — OpenRouter handles the instrumentation automatically.++## Setup++### 1. Get Your Traceway Project Token++AI Tracing works with any backend or OpenTelemetry project in Traceway. If you don't have a project yet, create a new one and select **OpenTelemetry** as the framework — this gives you a project token you can use for the OpenRouter integration.++In your Traceway dashboard, go to **Connection** and copy your project token.++### 2. Configure OpenRouter Observability++1. Go to [OpenRouter Settings → Observability](https://openrouter.ai/settings/integrations)+2. Enable **Broadcast**+3. Click **Add Destination** next to **OpenTelemetry Collector**+4. Fill in the connection details:++| Field | Value |+|-------|-------|+| **Name** | `Traceway` (or any label) |+| **Endpoint** | `https://cloud.tracewayapp.com/api/otel/v1/traces` |+| **Headers** | `{"Authorization": "Bearer YOUR_PROJECT_TOKEN"}` |++Replace `YOUR_PROJECT_TOKEN` with your actual project token. If you're self-hosting, replace the endpoint with your instance URL.++5. Click **Test Connection** to verify+6. Click **Send Trace** to send a test trace+7. Click **Add** to save++### 3. Verify in Traceway++Open your Traceway dashboard and navigate to **AI Traces**. You should see your test trace appear within a few seconds.++## Configuration Options++OpenRouter provides several configuration options for the observability destination:++### Privacy Mode++When enabled, OpenRouter excludes prompt and completion content from traces. Only metadata (model, tokens, cost, duration) is sent. Enable this if you don't want conversation data stored in Traceway.++### Sampling Rate++Controls what percentage of traces are sent to Traceway. Set to `1` (100%) to capture every call, or lower it to reduce volume. Default is `1`.++### API Key Filter++Optionally filter traces to only include calls made with specific API keys. Useful if you have multiple environments sharing the same OpenRouter organization.++## What Gets Captured++Every OpenRouter completion generates a trace with:++| Field | Source Attribute | Example |+|-------|-----------------|---------|+| Model | `gen_ai.request.model` | `openai/gpt-4-turbo` |+| Provider | `gen_ai.system` | `openai` |+| Operation | `gen_ai.operation.name` | `chat` |+| Input Tokens | `gen_ai.usage.input_tokens` | `50` |+| Output Tokens | `gen_ai.usage.output_tokens` | `100` |+| Cached Tokens | `gen_ai.usage.input_tokens.cached` | `20` |+| Reasoning Tokens | `gen_ai.usage.output_tokens.reasoning` | `10` |+| Input Cost | `gen_ai.usage.input_cost` | `0.005` |+| Output Cost | `gen_ai.usage.output_cost` | `0.015` |+| Total Cost | `gen_ai.usage.total_cost` | `0.020` |+| Finish Reason | `gen_ai.response.finish_reason` | `stop` |+| Prompt | `gen_ai.prompt` | Full messages JSON |+| Completion | `gen_ai.completion` | Full response JSON |+| User ID | `user.id` | Your OpenRouter user/org ID |+| Trace Name | `trace.name` | Workflow identifier |+| Temperature | `gen_ai.request.temperature` | `0.7` |++## Self-Hosted Setup++If you're running Traceway on your own infrastructure, use your instance URL as the endpoint:++```+https://your-traceway-instance.com/api/otel/v1/traces+```++The rest of the configuration is identical — project token in the Authorization header, same format.++## Next Steps++- [AI Tracing concepts](/learn/ai-tracing) — Understand how AI traces are stored and queried+- [OpenTelemetry](/client/otel) — Send traces from any OTel-instrumented applicationdiff --git a/docs/pages/learn/_meta.json b/docs/pages/learn/_meta.jsonindex 92c4d183..dcc471c0 100644--- a/docs/pages/learn/_meta.json+++ b/docs/pages/learn/_meta.json@@ -2,6 +2,7 @@"index": "Overview","data-flow": "Data Flow","traces": "Traces",+ "ai-tracing": "AI Tracing","issues": "Issues","attributes": "Attributes","spans": "Spans",diff --git a/docs/pages/learn/ai-tracing.mdx b/docs/pages/learn/ai-tracing.mdxnew file mode 100644index 00000000..f6376849--- /dev/null+++ b/docs/pages/learn/ai-tracing.mdx@@ -0,0 +1,87 @@+# AI Tracing++**AI Tracing** lets you monitor LLM and AI model calls across your application. Every API call to an AI provider — whether it's OpenAI, Anthropic, Cohere, or any model routed through OpenRouter — is captured as an AI trace with full cost, token usage, and conversation data.++## Why AI Observability Matters++AI API calls are fundamentally different from traditional HTTP requests:++- **Costs are unpredictable** — A single prompt with a large context window can cost 100x more than average. Without visibility, cost spikes go unnoticed until the invoice arrives.+- **Latency varies wildly** — The same model can respond in 500ms or 30 seconds depending on prompt length, model load, and provider routing. You need P50/P95 breakdowns to understand real-world performance.+- **Token usage drives cost** — Input and output tokens are billed separately at different rates. Cached tokens and reasoning tokens add further complexity. You need per-call granularity.+- **Prompt/completion quality matters** — Seeing the actual conversation helps debug unexpected model behavior, hallucinations, or prompt injection issues.+- **Provider reliability differs** — When routing through multiple providers, you need to know which provider is slower, more expensive, or failing more often.++## What Gets Captured++Every AI trace records:++| Field | Description |+|-------|-------------|+| Model | The requested model (e.g., `openai/gpt-4-turbo`) |+| Provider | The AI provider (e.g., `openai`, `anthropic`) |+| Duration | End-to-end latency of the API call |+| Input Tokens | Tokens in the prompt |+| Output Tokens | Tokens in the completion |+| Total Tokens | Combined token count |+| Cached Tokens | Tokens served from cache (reduced cost) |+| Reasoning Tokens | Tokens used for chain-of-thought reasoning |+| Input Cost | Cost of the prompt |+| Output Cost | Cost of the completion |+| Total Cost | Combined cost |+| Finish Reason | Why the model stopped (e.g., `stop`, `length`) |+| User ID | The user who triggered the call |+| Conversation | Full input/output content (stored separately) |++## How It Works++AI traces are ingested via the [OpenTelemetry](/client/otel) OTLP/HTTP endpoint. AI providers that support OTLP trace export (like [OpenRouter](/client/openrouter)) send spans with `gen_ai.*` semantic convention attributes. Traceway detects these attributes and stores them as AI traces.++The data flow:++1. **AI provider sends OTLP span** — The span contains `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.usage.total_cost`, and other standardized attributes+2. **Traceway parses gen_ai attributes** — The OTEL trace converter detects spans with `gen_ai.*` attributes and extracts structured fields+3. **Conversation content stored separately** — The full prompt and completion are stored in object storage (S3 or local filesystem) to keep the database lean+4. **Dashboard shows aggregated view** — AI traces are grouped by trace name (your agent or workflow identifier), with drill-down to individual calls++## Three-Level Drill-Down++AI traces follow the same drill-down pattern as [endpoints](/learn/traces#endpoints):++### Level 1: Grouped by Trace Name++See all your AI agents/workflows at a glance with aggregate metrics — total calls, P50/P95 duration, total tokens consumed, and total cost.++<img src="/ai-traces.png" alt="AI Traces Dashboard" style={{ maxWidth: '100%', height: 'auto', marginTop: '16px', borderRadius: '8px', border: '1px solid #e5e7eb' }} />++### Level 2: Individual Calls++Click a trace name to see every individual call — timestamp, duration, model used, tokens in/out, cost, and provider.++### Level 3: Call Detail++Click a specific call to see the full detail: token breakdown (including cached and reasoning tokens), cost breakdown, all metadata attributes, and the complete conversation with the model rendered as a chat view.++<img src="/ai-traces-detail.png" alt="AI Trace Detail" style={{ maxWidth: '100%', height: 'auto', marginTop: '16px', borderRadius: '8px', border: '1px solid #e5e7eb' }} />++## Conversation Storage++Conversation content (prompts and completions) can be large — a single prompt with RAG context might be 50KB+. To keep the database fast for aggregation queries, Traceway stores conversation content in object storage (S3 or local filesystem) and reads it on demand when you view a specific trace.++This means:+- **List views are fast** — No large text fields in aggregation queries+- **Detail views load conversations** — The full input/output is fetched from storage when you drill into a specific call+- **Privacy-friendly** — You can configure your AI provider to exclude prompt/completion data if needed++## Supported Providers++Any AI provider or gateway that exports OTLP traces with `gen_ai.*` semantic convention attributes works with Traceway. Currently tested:++| Provider | Integration |+|----------|-------------|+| [OpenRouter](/client/openrouter) | Native OTLP export via Observability settings |+| Any OpenTelemetry-instrumented app | Via `gen_ai.*` semantic conventions |++## Getting Started++See the [OpenRouter integration guide](/client/openrouter) for a step-by-step setup, or the [OpenTelemetry guide](/client/otel) if you're instrumenting your own AI calls.diff --git a/docs/public/ai-traces-detail.png b/docs/public/ai-traces-detail.pngnew file mode 100644index 00000000..247a51d7Binary files /dev/null and b/docs/public/ai-traces-detail.png differdiff --git a/docs/public/ai-traces.png b/docs/public/ai-traces.pngnew file mode 100644index 00000000..871db200Binary files /dev/null and b/docs/public/ai-traces.png differdiff --git a/docs/public/openrouter.png b/docs/public/openrouter.pngnew file mode 100644index 00000000..48f4a9a1Binary files /dev/null and b/docs/public/openrouter.png differdiff --git a/docs/theme.config.jsx b/docs/theme.config.jsxindex 7e6488ac..a56c4b9f 100644--- a/docs/theme.config.jsx+++ b/docs/theme.config.jsx@@ -19,6 +19,7 @@ const SDK_VISIBILITY = {svelte: "js-svelte",jquery: "js-jquery","js-sdk": "js-",+ openrouter: "openrouter",otel: "otel",cloudflare: "cloudflare",symfony: "php-symfony",diff --git a/frontend/src/lib/components/app-sidebar.svelte b/frontend/src/lib/components/app-sidebar.svelteindex 83b664fe..e394e560 100644--- a/frontend/src/lib/components/app-sidebar.svelte+++ b/frontend/src/lib/components/app-sidebar.svelte@@ -3,6 +3,7 @@import { useSidebar } from '$lib/components/ui/sidebar';import {Bell,+ Workflow,Bug,Link2,ChartNoAxesCombined,@@ -27,7 +28,7 @@external?: boolean;}- const hiddenForFrontend = new Set(['Dashboard', 'Endpoints', 'Tasks', 'Metrics']);+ const hiddenForFrontend = new Set(['Dashboard', 'Endpoints', 'Tasks', 'Metrics', 'AI Traces']);const hiddenForCloudflare = new Set(['Metrics']);const allSidebarItems: SidebarItem[] = [@@ -35,6 +36,7 @@{ Icon: Bug, href: '/issues', title: 'Issues', stickyParams: ['preset', 'from', 'to'] },{ Icon: Gauge, href: '/endpoints', title: 'Endpoints', stickyParams: ['preset', 'from', 'to'] },{ Icon: ListEnd, href: '/tasks', title: 'Tasks', stickyParams: ['preset', 'from', 'to'] },+ { Icon: Workflow, href: '/ai-traces', title: 'AI Traces', stickyParams: ['preset', 'from', 'to'] },{Icon: ChartNoAxesCombined,href: '/metrics',diff --git a/frontend/src/lib/components/ui/attributes-grid/attributes-grid.svelte b/frontend/src/lib/components/ui/attributes-grid/attributes-grid.svelteindex 8a661714..08987b0e 100644--- a/frontend/src/lib/components/ui/attributes-grid/attributes-grid.svelte+++ b/frontend/src/lib/components/ui/attributes-grid/attributes-grid.svelte@@ -3,12 +3,16 @@let {attributes,- sorted = true+ sorted = true,+ collapsedCount = 3}: {attributes: Record<string, string>;sorted?: boolean;+ collapsedCount?: number;} = $props();+ let expanded = $state(false);+const entries = $derived(() => {const items = Object.entries(attributes);if (sorted) {@@ -16,10 +20,43 @@}return items;});++ const visibleEntries = $derived(() => {+ const all = entries();+ if (expanded || all.length <= collapsedCount) return all;+ return all.slice(0, collapsedCount);+ });++ const hiddenCount = $derived(() => {+ const all = entries();+ if (expanded || all.length <= collapsedCount) return 0;+ return all.length - collapsedCount;+ });</script>-<div class="grid grid-cols-1 gap-3 sm:grid-cols-2 md:grid-cols-3">- {#each entries() as [key, value]}- <AttributesView title={key} {value} />- {/each}+<div>+ <div class="grid grid-cols-1 gap-3 sm:grid-cols-2 md:grid-cols-3">+ {#each visibleEntries() as [key, value]}+ <AttributesView title={key} {value} />+ {/each}+ </div>+ {#if hiddenCount() > 0}+ <div class="flex justify-end">+ <button+ onclick={() => expanded = true}+ class="mt-2 text-xs text-muted-foreground hover:text-foreground transition-colors"+ >+ Show {hiddenCount()} more...+ </button>+ </div>+ {:else if expanded && entries().length > collapsedCount}+ <div class="flex justify-end">+ <button+ onclick={() => expanded = false}+ class="mt-2 text-xs text-muted-foreground hover:text-foreground transition-colors"+ >+ Show less+ </button>+ </div>+ {/if}</div>diff --git a/frontend/src/routes/ai-traces/+page.svelte b/frontend/src/routes/ai-traces/+page.sveltenew file mode 100644index 00000000..d166b4b9--- /dev/null+++ b/frontend/src/routes/ai-traces/+page.svelte@@ -0,0 +1,407 @@+<script lang="ts">+ import { onMount, onDestroy } from 'svelte';+ import { api } from '$lib/api';+ import { formatDuration, toUTCISO, calendarDateTimeToLuxon } from '$lib/utils/formatters';+ import { getTimezone } from '$lib/state/timezone.svelte';+ import * as Table from "$lib/components/ui/table";+ import { LoadingCircle } from "$lib/components/ui/loading-circle";+ import { TracewayTableHeader } from "$lib/components/ui/traceway-table-header";+ import { TableEmptyState } from "$lib/components/ui/table-empty-state";+ import { PaginationFooter } from "$lib/components/ui/pagination-footer";+ import { TimeRangePicker } from "$lib/components/ui/time-range-picker";+ import { SearchBar } from "$lib/components/ui/search-bar";+ import { browser } from '$app/environment';+ import { CalendarDate } from "@internationalized/date";+ import { projectsState } from '$lib/state/projects.svelte';+ import { createRowClickHandler } from '$lib/utils/navigation';+ import { resolve } from '$app/paths';+ import PageHeader from '$lib/components/issues/page-header.svelte';+ import {+ presetMinutes,+ getTimeRangeFromPreset,+ dateToCalendarDate,+ dateToTimeString,+ parseTimeRangeFromUrl,+ getResolvedTimeRange,+ updateUrl+ } from '$lib/utils/url-params';+ import {+ getSortState,+ setSortState,+ handleSortClick,+ type SortDirection+ } from '$lib/utils/sort-storage';++ const timezone = $derived(getTimezone());++ type AiTraceStats = {+ traceName: string;+ count: number;+ p50Duration: number;+ p95Duration: number;+ avgDuration: number;+ totalTokens: number;+ totalCost: number;+ avgInputTokens: number;+ avgOutputTokens: number;+ lastSeen: string;+ };++ type SortField = 'count' | 'p50_duration' | 'p95_duration' | 'total_tokens' | 'total_cost' | 'last_seen';++ let traces = $state<AiTraceStats[]>([]);+ let loading = $state(true);+ let error = $state('');++ // Pagination State+ let page = $state(1);+ let pageSize = $state(50);+ let total = $state(0);+ let totalPages = $state(0);++ // Parse URL params including search+ function parseAiTracesUrlParams() {+ if (!browser) return { preset: '24h', from: null, to: null, search: '' };+ const params = new URLSearchParams(window.location.search);+ const timeParams = parseTimeRangeFromUrl(timezone, '24h');+ return {+ ...timeParams,+ search: params.get('search') || ''+ };+ }++ // Initialize from URL+ const initialUrlParams = parseAiTracesUrlParams();+ const initialRange = getResolvedTimeRange(initialUrlParams, timezone);++ // Search state+ let searchQuery = $state(initialUrlParams.search);++ // Date Range State+ let selectedPreset = $state<string | null>(initialUrlParams.preset);+ let fromDate = $state<CalendarDate>(dateToCalendarDate(initialRange.from, timezone));+ let toDate = $state<CalendarDate>(dateToCalendarDate(initialRange.to, timezone));+ let fromTime = $state(dateToTimeString(initialRange.from, timezone));+ let toTime = $state(dateToTimeString(initialRange.to, timezone));++ // Update URL with current time range and search+ function updateTimeRangeUrl(pushToHistory = true) {+ const params: Record<string, string | null | undefined> = {};+ if (selectedPreset) {+ params.preset = selectedPreset;+ } else {+ params.from = getFromDateTimeUTC();+ params.to = getToDateTimeUTC();+ }+ if (searchQuery.trim()) {+ params.search = searchQuery.trim();+ }+ updateUrl(params, { pushToHistory });+ }++ // Handle browser back/forward navigation+ function handlePopState() {+ const urlParams = parseAiTracesUrlParams();+ const range = getResolvedTimeRange(urlParams, timezone);++ selectedPreset = urlParams.preset;+ fromDate = dateToCalendarDate(range.from, timezone);+ fromTime = dateToTimeString(range.from, timezone);+ toDate = dateToCalendarDate(range.to, timezone);+ toTime = dateToTimeString(range.to, timezone);+ searchQuery = urlParams.search;++ page = 1;+ loadData(false);+ }++ // Sorting - persisted to localStorage+ const SORT_STORAGE_KEY = 'ai-traces';+ const initialSort = getSortState(SORT_STORAGE_KEY, { field: 'total_cost', direction: 'desc' });+ let orderBy = $state<SortField>(initialSort.field as SortField);+ let sortDirection = $state<SortDirection>(initialSort.direction);++ // Combine date and time into UTC ISO datetime string+ function getFromDateTimeUTC(): string {+ const [hour, minute] = (fromTime || '00:00').split(':').map(Number);+ const dt = calendarDateTimeToLuxon({ year: fromDate.year, month: fromDate.month, day: fromDate.day, hour, minute }, timezone);+ return toUTCISO(dt);+ }++ function getToDateTimeUTC(): string {+ const [hour, minute] = (toTime || '23:59').split(':').map(Number);+ const dt = calendarDateTimeToLuxon({ year: toDate.year, month: toDate.month, day: toDate.day, hour, minute }, timezone).endOf('minute');+ return toUTCISO(dt);+ }++ function handleTimeRangeChange(from: { date: CalendarDate; time: string }, to: { date: CalendarDate; time: string }, preset: string | null) {+ fromDate = from.date;+ fromTime = from.time;+ toDate = to.date;+ toTime = to.time;+ selectedPreset = preset;+ page = 1;+ loadData(false);+ }++ function formatCost(cost: number): string {+ if (cost === 0) return '$0';+ if (cost < 0.001) return `$${cost.toFixed(6)}`;+ if (cost < 0.01) return `$${cost.toFixed(4)}`;+ if (cost < 1) return `$${cost.toFixed(3)}`;+ return `$${cost.toFixed(2)}`;+ }++ function formatCount(count: number): string {+ if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}m`;+ if (count >= 1_000) return `${(count / 1_000).toFixed(1)}k`;+ return count.toLocaleString();+ }++ function formatTokens(tokens: number): string {+ if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M`;+ if (tokens >= 1_000) return `${(tokens / 1_000).toFixed(1)}k`;+ return tokens.toLocaleString();+ }++ async function loadData(pushToHistory = true) {+ loading = true;+ error = '';++ if (selectedPreset) {+ const range = getTimeRangeFromPreset(selectedPreset, timezone);+ fromDate = dateToCalendarDate(range.from, timezone);+ toDate = dateToCalendarDate(range.to, timezone);+ fromTime = dateToTimeString(range.from, timezone);+ toTime = dateToTimeString(range.to, timezone);+ }++ // Update URL+ updateTimeRangeUrl(pushToHistory);++ try {+ const requestBody = {+ fromDate: getFromDateTimeUTC(),+ toDate: getToDateTimeUTC(),+ orderBy: orderBy,+ sortDirection: sortDirection,+ pagination: {+ page: page,+ pageSize: pageSize+ },+ search: searchQuery.trim()+ };++ const response = await api.post('/ai-traces/grouped', requestBody, { projectId: projectsState.currentProjectId ?? undefined });++ traces = response.data || [];+ total = response.pagination.total;+ totalPages = response.pagination.totalPages;+ } catch (e: any) {+ console.error(e);+ error = e.message || 'Failed to load data';+ } finally {+ loading = false;+ }+ }++ function handlePageChange(newPage: number) {+ if (newPage >= 1 && newPage <= totalPages) {+ page = newPage;+ loadData(false);+ }+ }++ function handlePageSizeChange(newPageSize: number) {+ pageSize = newPageSize;+ page = 1;+ loadData(false);+ }++ function handleSort(field: SortField) {+ const newSort = handleSortClick(field, orderBy, sortDirection);+ orderBy = newSort.field as SortField;+ sortDirection = newSort.direction;+ setSortState(SORT_STORAGE_KEY, newSort);+ page = 1;+ loadData(false);+ }++ function handleSearch() {+ page = 1;+ loadData(true);+ }++ onMount(() => {+ // Add popstate listener for back/forward navigation+ window.addEventListener('popstate', handlePopState);++ // Initial load with replaceState (don't push to history)+ loadData(false);+ });++ onDestroy(() => {+ if (typeof window !== 'undefined') {+ window.removeEventListener('popstate', handlePopState);+ }+ });+</script>++<div class="space-y-4">+ <!-- Header with Title and Time Range Filter -->+ <div class="flex flex-col gap-4 sm:flex-row sm:justify-between">+ <PageHeader title="AI Traces" />+ <div class="flex flex-col">+ <TimeRangePicker+ bind:fromDate+ bind:toDate+ bind:fromTime+ bind:toTime+ bind:preset={selectedPreset}+ onApply={handleTimeRangeChange}+ />+ </div>+ </div>++ <!-- Search -->+ <SearchBar+ placeholder="Search traces..."+ bind:value={searchQuery}+ onSearch={handleSearch}+ disabled={loading}+ />++ <!-- AI Traces Table -->+ <div class="rounded-md border overflow-hidden">+ <Table.Root>+ {#if loading}+ <Table.Body>+ <Table.Row>+ <Table.Cell colspan={7} class="h-48">+ <div class="flex justify-center items-center h-full">+ <LoadingCircle size="xlg" />+ </div>+ </Table.Cell>+ </Table.Row>+ </Table.Body>+ {:else if error}+ <Table.Body>+ <Table.Row>+ <Table.Cell colspan={7} class="h-24 text-center text-red-500">+ {error}+ </Table.Cell>+ </Table.Row>+ </Table.Body>+ {:else if traces.length === 0}+ <Table.Body>+ <TableEmptyState colspan={7} message="No AI trace data available for your search parameters" />+ </Table.Body>+ {:else}+ <Table.Header>+ <Table.Row>+ <TracewayTableHeader+ label="Trace Name"+ tooltip="The AI agent or workflow name"+ />+ <TracewayTableHeader+ label="Calls"+ tooltip="Total number of AI calls"+ sortField="count"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[80px]"+ />+ <TracewayTableHeader+ label="Typical"+ tooltip="Median duration (P50)"+ sortField="p50_duration"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[100px]"+ />+ <TracewayTableHeader+ label="Slow"+ tooltip="95th percentile duration"+ sortField="p95_duration"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[100px]"+ />+ <TracewayTableHeader+ label="Total Tokens"+ tooltip="Total tokens consumed"+ sortField="total_tokens"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[110px]"+ />+ <TracewayTableHeader+ label="Total Cost"+ tooltip="Total cost across all calls"+ sortField="total_cost"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[100px]"+ />+ <TracewayTableHeader+ label="Last Seen"+ tooltip="When the last call occurred"+ sortField="last_seen"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[100px]"+ />+ </Table.Row>+ </Table.Header>+ <Table.Body>+ {#each traces as trace}+ <Table.Row+ class="cursor-pointer hover:bg-muted/50"+ onclick={createRowClickHandler(resolve(`/ai-traces/${encodeURIComponent(trace.traceName)}`), 'preset', 'from', 'to')}+ >+ <Table.Cell class="font-mono text-sm">+ {trace.traceName}+ </Table.Cell>+ <Table.Cell class="tabular-nums">+ {formatCount(trace.count)}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm tabular-nums">+ {formatDuration(trace.p50Duration)}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm tabular-nums">+ {formatDuration(trace.p95Duration)}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm tabular-nums">+ {formatTokens(trace.totalTokens)}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm tabular-nums">+ {formatCost(trace.totalCost)}+ </Table.Cell>+ <Table.Cell class="text-muted-foreground text-sm">+ {new Date(trace.lastSeen).toLocaleDateString()}+ </Table.Cell>+ </Table.Row>+ {/each}+ </Table.Body>+ {/if}+ </Table.Root>+ </div>++ <!-- Pagination Footer -->+ <PaginationFooter+ currentPage={page}+ {totalPages}+ {pageSize}+ totalItems={total}+ onPageChange={handlePageChange}+ onPageSizeChange={handlePageSizeChange}+ {loading}+ itemLabel="trace"+ />+</div>diff --git a/frontend/src/routes/ai-traces/+page.ts b/frontend/src/routes/ai-traces/+page.tsnew file mode 100644index 00000000..9f2ce0c4--- /dev/null+++ b/frontend/src/routes/ai-traces/+page.ts@@ -0,0 +1,9 @@+import { redirect } from '@sveltejs/kit';+import { authState } from '$lib/state/auth.svelte';+import type { PageLoad } from './$types';++export const load: PageLoad = () => {+ if (!authState.isAuthenticated) {+ throw redirect(302, '/login');+ }+};diff --git a/frontend/src/routes/ai-traces/[traceName]/+page.svelte b/frontend/src/routes/ai-traces/[traceName]/+page.sveltenew file mode 100644index 00000000..2be11229--- /dev/null+++ b/frontend/src/routes/ai-traces/[traceName]/+page.svelte@@ -0,0 +1,399 @@+<script lang="ts">+ import { onMount } from 'svelte';+ import { api } from '$lib/api';+ import {+ formatDuration,+ formatDurationMs,+ formatDateTime,+ parseISO,+ toUTCISO,+ calendarDateTimeToLuxon+ } from '$lib/utils/formatters';+ import { getTimezone } from '$lib/state/timezone.svelte';+ import * as Table from '$lib/components/ui/table';+ import { LoadingCircle } from '$lib/components/ui/loading-circle';+ import { TimeRangePicker } from '$lib/components/ui/time-range-picker';+ import { TracewayTableHeader } from '$lib/components/ui/traceway-table-header';+ import { TableEmptyState } from '$lib/components/ui/table-empty-state';+ import { CalendarDate } from '@internationalized/date';+ import { ErrorDisplay } from '$lib/components/ui/error-display';+ import { projectsState } from '$lib/state/projects.svelte';+ import { createRowClickHandler } from '$lib/utils/navigation';+ import { createSmartBackHandler } from '$lib/utils/back-navigation';+ import PaginationFooter from '$lib/components/ui/pagination-footer/pagination-footer.svelte';+ import PageHeader from '$lib/components/issues/page-header.svelte';+ import { resolve } from '$app/paths';+ import {+ presetMinutes,+ getTimeRangeFromPreset,+ dateToCalendarDate,+ dateToTimeString,+ updateUrl+ } from '$lib/utils/url-params';+ import {+ getSortState,+ setSortState,+ handleSortClick,+ type SortDirection+ } from '$lib/utils/sort-storage';++ const timezone = $derived(getTimezone());++ type AiTrace = {+ id: string;+ recordedAt: string;+ duration: number;+ model: string;+ provider: string;+ inputTokens: number;+ outputTokens: number;+ totalTokens: number;+ totalCost: number;+ statusCode: number;+ };++ type AiTraceDetailStats = {+ count: number;+ avgDuration: number;+ medianDuration: number;+ p95Duration: number;+ totalTokens: number;+ totalCost: number;+ avgInputTokens: number;+ avgOutputTokens: number;+ throughput: number;+ };++ type SortField = 'recorded_at' | 'duration' | 'total_tokens' | 'total_cost';++ let { data } = $props();++ let traces = $state<AiTrace[]>([]);+ let stats = $state<AiTraceDetailStats | null>(null);+ let loading = $state(true);+ let error = $state('');+ let notFound = $state(false);+ let errorStatus = $state<number>(0);++ let page = $state(1);+ let pageSize = $state(50);+ let total = $state(0);+ let totalPages = $state(0);++ function getInitialRange(): { preset: string | null; from: Date; to: Date } {+ if (data.preset && presetMinutes[data.preset]) {+ const range = getTimeRangeFromPreset(data.preset, timezone);+ return { preset: data.preset, from: range.from, to: range.to };+ }+ if (data.from && data.to) {+ const fromDt = parseISO(data.from, timezone);+ const toDt = parseISO(data.to, timezone);+ if (fromDt.isValid && toDt.isValid) {+ return { preset: null, from: fromDt.toJSDate(), to: toDt.toJSDate() };+ }+ }+ const range = getTimeRangeFromPreset('24h', timezone);+ return { preset: '24h', from: range.from, to: range.to };+ }++ const initialRange = getInitialRange();++ let selectedPreset = $state<string | null>(initialRange.preset);+ let fromDate = $state<CalendarDate>(dateToCalendarDate(initialRange.from, timezone));+ let toDate = $state<CalendarDate>(dateToCalendarDate(initialRange.to, timezone));+ let fromTime = $state(dateToTimeString(initialRange.from, timezone));+ let toTime = $state(dateToTimeString(initialRange.to, timezone));++ function updateTimeRangeUrl(pushToHistory = true) {+ updateUrl(+ selectedPreset+ ? { preset: selectedPreset }+ : { from: getFromDateTimeUTC(), to: getToDateTimeUTC() },+ { pushToHistory }+ );+ }++ const SORT_STORAGE_KEY = 'ai_trace_detail';+ const initialSort = getSortState(SORT_STORAGE_KEY, { field: 'recorded_at', direction: 'desc' });+ let orderBy = $state<SortField>(initialSort.field as SortField);+ let sortDirection = $state<SortDirection>(initialSort.direction);++ function getFromDateTimeUTC(): string {+ const [hour, minute] = (fromTime || '00:00').split(':').map(Number);+ const dt = calendarDateTimeToLuxon(+ { year: fromDate.year, month: fromDate.month, day: fromDate.day, hour, minute },+ timezone+ );+ return toUTCISO(dt);+ }++ function getToDateTimeUTC(): string {+ const [hour, minute] = (toTime || '23:59').split(':').map(Number);+ const dt = calendarDateTimeToLuxon(+ { year: toDate.year, month: toDate.month, day: toDate.day, hour, minute },+ timezone+ ).endOf('minute');+ return toUTCISO(dt);+ }++ function handleTimeRangeChange(+ from: { date: CalendarDate; time: string },+ to: { date: CalendarDate; time: string },+ preset: string | null+ ) {+ fromDate = from.date;+ fromTime = from.time;+ toDate = to.date;+ toTime = to.time;+ selectedPreset = preset;+ page = 1;+ loadData();+ }++ function formatCost(cost: number): string {+ if (cost === 0) return '$0';+ if (cost < 0.001) return `$${cost.toFixed(6)}`;+ if (cost < 0.01) return `$${cost.toFixed(4)}`;+ if (cost < 1) return `$${cost.toFixed(3)}`;+ return `$${cost.toFixed(2)}`;+ }++ function formatTokens(tokens: number): string {+ if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M`;+ if (tokens >= 1_000) return `${(tokens / 1_000).toFixed(1)}k`;+ return tokens.toLocaleString();+ }++ async function loadData(pushToHistory = true) {+ loading = true;+ error = '';+ notFound = false;+ errorStatus = 0;++ updateTimeRangeUrl(pushToHistory);++ try {+ const requestBody = {+ fromDate: getFromDateTimeUTC(),+ toDate: getToDateTimeUTC(),+ orderBy: orderBy,+ sortDirection: sortDirection,+ pagination: { page, pageSize }+ };++ const response = await api.post(+ `/ai-traces/trace?traceName=${encodeURIComponent(data.traceName)}`,+ requestBody,+ { projectId: projectsState.currentProjectId ?? undefined }+ );++ traces = response.data || [];+ stats = response.stats || null;+ total = response.pagination.total;+ totalPages = response.pagination.totalPages;+ } catch (e: any) {+ console.error(e);+ errorStatus = e.status || 0;+ if (e.status === 404) {+ notFound = true;+ } else {+ error = e.message || 'Failed to load data';+ }+ } finally {+ loading = false;+ }+ }++ function handlePageChange(newPage: number) {+ if (newPage >= 1 && newPage <= totalPages) {+ page = newPage;+ loadData(false);+ }+ }++ function handlePageSizeChange(newPageSize: number) {+ pageSize = newPageSize;+ page = 1;+ loadData(false);+ }++ function handleSort(field: SortField) {+ const newSort = handleSortClick(field, orderBy, sortDirection);+ orderBy = newSort.field as SortField;+ sortDirection = newSort.direction;+ setSortState(SORT_STORAGE_KEY, newSort);+ page = 1;+ loadData(false);+ }++ onMount(() => {+ loadData(false);+ });+</script>++<div class="space-y-6">+ {#if notFound}+ <ErrorDisplay+ status={404}+ title="AI Trace Not Found"+ description="The AI trace you're looking for doesn't exist or has no recorded calls."+ onBack={createSmartBackHandler({ fallbackPath: resolve('/ai-traces') })}+ backLabel="Back to AI Traces"+ onRetry={() => loadData(false)}+ identifier={decodeURIComponent(data.traceName)}+ />+ {:else if error && !loading}+ <ErrorDisplay+ status={errorStatus === 400 ? 400 : 400}+ title="Failed to Load AI Traces"+ description={error}+ onBack={createSmartBackHandler({ fallbackPath: resolve('/ai-traces') })}+ backLabel="Back to AI Traces"+ onRetry={() => loadData(false)}+ />+ {:else}+ <div class="flex flex-col gap-4 sm:flex-row sm:justify-between">+ <PageHeader+ title={decodeURIComponent(data.traceName)}+ subtitle="AI trace instances"+ onBack={createSmartBackHandler({ fallbackPath: resolve('/ai-traces') })}+ />+ <div class="flex flex-col">+ <TimeRangePicker+ bind:fromDate+ bind:toDate+ bind:fromTime+ bind:toTime+ bind:preset={selectedPreset}+ onApply={handleTimeRangeChange}+ />+ </div>+ </div>++ {#if stats}+ <div class="grid grid-cols-2 gap-4 md:grid-cols-4 lg:grid-cols-5">+ <div class="space-y-1">+ <p class="text-2xl font-semibold tracking-tight">{formatDurationMs(stats.avgDuration)}</p>+ <p class="text-xs text-muted-foreground">Average duration</p>+ </div>+ <div class="space-y-1">+ <p class="text-2xl font-semibold tracking-tight">{formatDurationMs(stats.medianDuration)}</p>+ <p class="text-xs text-muted-foreground">Median duration</p>+ </div>+ <div class="space-y-1">+ <p class="text-2xl font-semibold tracking-tight">{formatDurationMs(stats.p95Duration)}</p>+ <p class="text-xs text-muted-foreground">95th percentile</p>+ </div>+ <div class="space-y-1">+ <p class="text-2xl font-semibold tracking-tight">{formatCost(stats.totalCost)}</p>+ <p class="text-xs text-muted-foreground">Total cost</p>+ </div>+ <div class="space-y-1">+ <p class="text-2xl font-semibold tracking-tight">{formatTokens(stats.totalTokens)}</p>+ <p class="text-xs text-muted-foreground">Total tokens</p>+ </div>+ </div>+ {:else if loading}+ <div class="flex items-center justify-center py-8">+ <LoadingCircle size="lg" />+ </div>+ {/if}++ <div class="overflow-hidden rounded-md border">+ <Table.Root>+ {#if loading || traces.length > 0}+ <Table.Header>+ <Table.Row>+ <TracewayTableHeader+ label="Recorded At"+ sortField="recorded_at"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[180px]"+ />+ <TracewayTableHeader+ label="Duration"+ sortField="duration"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[100px]"+ />+ <TracewayTableHeader label="Model" class="w-[200px]" />+ <TracewayTableHeader label="Tokens In" class="w-[90px]" />+ <TracewayTableHeader label="Tokens Out" class="w-[90px]" />+ <TracewayTableHeader+ label="Cost"+ sortField="total_cost"+ currentSortField={orderBy}+ {sortDirection}+ onSort={(field) => handleSort(field as SortField)}+ class="w-[90px]"+ />+ <TracewayTableHeader label="Provider" class="w-[100px]" />+ </Table.Row>+ </Table.Header>+ {/if}+ <Table.Body>+ {#if loading}+ <Table.Row>+ <Table.Cell colspan={7} class="h-48">+ <div class="flex items-center justify-center">+ <LoadingCircle size="lg" />+ </div>+ </Table.Cell>+ </Table.Row>+ {:else if traces.length === 0}+ <TableEmptyState colspan={7} message="No AI traces found in this time range." />+ {:else}+ {#each traces as trace}+ <Table.Row+ class="cursor-pointer hover:bg-muted/50"+ onclick={createRowClickHandler(+ `/ai-traces/${encodeURIComponent(decodeURIComponent(data.traceName))}/${trace.id}`,+ 'preset',+ 'from',+ 'to'+ )}+ >+ <Table.Cell class="text-muted-foreground">+ {formatDateTime(trace.recordedAt, { timezone })}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm">+ {formatDuration(trace.duration)}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm">+ {trace.model || '-'}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm tabular-nums">+ {trace.inputTokens.toLocaleString()}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm tabular-nums">+ {trace.outputTokens.toLocaleString()}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm tabular-nums">+ {formatCost(trace.totalCost)}+ </Table.Cell>+ <Table.Cell class="font-mono text-sm text-muted-foreground">+ {trace.provider || '-'}+ </Table.Cell>+ </Table.Row>+ {/each}+ {/if}+ </Table.Body>+ </Table.Root>+ </div>++ <PaginationFooter+ currentPage={page}+ {totalPages}+ {pageSize}+ totalItems={total}+ onPageChange={handlePageChange}+ onPageSizeChange={handlePageSizeChange}+ {loading}+ itemLabel="trace"+ />+ {/if}+</div>diff --git a/frontend/src/routes/ai-traces/[traceName]/+page.ts b/frontend/src/routes/ai-traces/[traceName]/+page.tsnew file mode 100644index 00000000..70bffd06--- /dev/null+++ b/frontend/src/routes/ai-traces/[traceName]/+page.ts@@ -0,0 +1,18 @@+import { redirect } from '@sveltejs/kit';+import { authState } from '$lib/state/auth.svelte';+import type { PageLoad } from './$types';++export const prerender = false;++export const load: PageLoad = ({ params, url }) => {+ if (!authState.isAuthenticated) {+ throw redirect(302, '/login');+ }++ return {+ traceName: params.traceName,+ preset: url.searchParams.get('preset') || null,+ from: url.searchParams.get('from') || null,+ to: url.searchParams.get('to') || null+ };+};diff --git a/frontend/src/routes/ai-traces/[traceName]/[traceId]/+page.svelte b/frontend/src/routes/ai-traces/[traceName]/[traceId]/+page.sveltenew file mode 100644index 00000000..a6604166--- /dev/null+++ b/frontend/src/routes/ai-traces/[traceName]/[traceId]/+page.svelte@@ -0,0 +1,358 @@+<script lang="ts">+ import { onMount } from 'svelte';+ import { api } from '$lib/api';+ import { formatDuration, formatDateTime } from '$lib/utils/formatters';+ import { getTimezone } from '$lib/state/timezone.svelte';+ import * as Card from '$lib/components/ui/card';+ import { LoadingCircle } from '$lib/components/ui/loading-circle';+ import { ErrorDisplay } from '$lib/components/ui/error-display';+ import { projectsState } from '$lib/state/projects.svelte';+ import { LabelValue } from '$lib/components/ui/label-value';+ import { AttributesGrid } from '$lib/components/ui/attributes-grid/index.js';+ import PageHeader from '$lib/components/issues/page-header.svelte';+ import { createSmartBackHandler } from '$lib/utils/back-navigation';+ import { resolve } from '$app/paths';++ type AiTrace = {+ id: string;+ recordedAt: string;+ duration: number;+ statusCode: number;+ model: string;+ responseModel: string;+ provider: string;+ operation: string;+ inputTokens: number;+ outputTokens: number;+ totalTokens: number;+ cachedTokens: number;+ reasoningTokens: number;+ inputCost: number;+ outputCost: number;+ totalCost: number;+ traceName: string;+ userId: string;+ finishReason: string;+ serverName: string;+ appVersion: string;+ attributes: Record<string, string> | null;+ };++ type AiTraceDetailResponse = {+ aiTrace: AiTrace;+ conversation?: {+ input: string;+ output: string;+ };+ };++ let { data } = $props();++ const timezone = $derived(getTimezone());++ let response = $state<AiTraceDetailResponse | null>(null);+ let loading = $state(true);+ let error = $state('');+ let notFound = $state(false);++ function formatCost(cost: number): string {+ if (cost === 0) return '$0';+ if (cost < 0.001) return `$${cost.toFixed(6)}`;+ if (cost < 0.01) return `$${cost.toFixed(4)}`;+ if (cost < 1) return `$${cost.toFixed(3)}`;+ return `$${cost.toFixed(2)}`;+ }++ type ChatMessage = {+ role: string;+ content: string | ContentPart[];+ };++ type ContentPart = {+ type: string;+ text?: string;+ image_url?: { url: string };+ };++ function tryParseJson(str: string): any {+ try {+ return JSON.parse(str);+ } catch {+ return null;+ }+ }++ function extractMessages(input: string, output: string): ChatMessage[] | null {+ const inputParsed = tryParseJson(input);+ if (!inputParsed) return null;++ const messages: ChatMessage[] = [];++ // Extract input messages — could be {messages: [...]} or just [...]+ const inputMessages = inputParsed?.messages ?? (Array.isArray(inputParsed) ? inputParsed : null);+ if (!Array.isArray(inputMessages)) return null;++ for (const msg of inputMessages) {+ if (msg.role && msg.content !== undefined) {+ messages.push({ role: msg.role, content: msg.content });+ }+ }++ // Extract assistant response from output+ const outputParsed = tryParseJson(output);+ if (outputParsed) {+ // OpenAI-style: {choices: [{message: {role, content}}]}+ const choices = outputParsed?.choices;+ if (Array.isArray(choices) && choices.length > 0) {+ const choice = choices[0];+ const msg = choice?.message;+ if (msg?.content) {+ messages.push({ role: msg.role || 'assistant', content: msg.content });+ }+ }+ // Anthropic-style: {content: [{type: "text", text: "..."}]}+ else if (Array.isArray(outputParsed?.content)) {+ const text = outputParsed.content+ .filter((c: any) => c.type === 'text')+ .map((c: any) => c.text)+ .join('');+ if (text) {+ messages.push({ role: outputParsed.role || 'assistant', content: text });+ }+ }+ }++ return messages.length > 0 ? messages : null;+ }++ function getMessageText(content: string | ContentPart[]): string {+ if (typeof content === 'string') return content;+ if (Array.isArray(content)) {+ return content+ .filter((p) => p.type === 'text' && p.text)+ .map((p) => p.text!)+ .join('\n');+ }+ return '';+ }++ function getMessageImages(content: string | ContentPart[]): string[] {+ if (!Array.isArray(content)) return [];+ return content+ .filter((p) => p.type === 'image_url' && p.image_url?.url)+ .map((p) => p.image_url!.url);+ }++ function getRoleLabel(role: string): string {+ switch (role) {+ case 'system': return 'System';+ case 'user': return 'User';+ case 'assistant': return 'Assistant';+ case 'tool': return 'Tool';+ case 'function': return 'Function';+ default: return role;+ }+ }++ function formatConversationContent(raw: string): string {+ const parsed = tryParseJson(raw);+ if (parsed) {+ return JSON.stringify(parsed, null, 2);+ }+ return raw;+ }++ let showRawJson = $state(false);++ async function loadData() {+ loading = true;+ error = '';+ notFound = false;++ try {+ const result = await api.post(+ `/ai-traces/${data.traceId}`,+ {},+ { projectId: projectsState.currentProjectId ?? undefined }+ );+ response = result;+ } catch (e: unknown) {+ console.error(e);+ const err = e as { status?: number; message?: string };+ if (err.status === 404) {+ notFound = true;+ } else {+ error = err.message || 'Failed to load AI trace details';+ }+ } finally {+ loading = false;+ }+ }++ onMount(() => {+ loadData();+ });+</script>++<div class="space-y-6">+ <PageHeader+ title={decodeURIComponent(data.traceName)}+ subtitle={`Trace ID: ${data.traceId}`}+ onBack={createSmartBackHandler({+ fallbackPath: resolve('/ai-traces/[traceName]', { traceName: encodeURIComponent(data.traceName) })+ })}+ />++ {#if loading}+ <div class="flex items-center justify-center py-20">+ <LoadingCircle size="xlg" />+ </div>+ {:else if notFound}+ <ErrorDisplay+ status={404}+ title="AI Trace Not Found"+ description="The AI trace you're looking for doesn't exist or may have expired."+ onBack={createSmartBackHandler({+ fallbackPath: resolve('/ai-traces/[traceName]', { traceName: encodeURIComponent(data.traceName) })+ })}+ backLabel="Back to Trace"+ onRetry={loadData}+ identifier={data.traceId}+ />+ {:else if error}+ <ErrorDisplay+ status={400}+ title="Failed to Load AI Trace"+ description={error}+ onBack={createSmartBackHandler({+ fallbackPath: resolve('/ai-traces/[traceName]', { traceName: encodeURIComponent(data.traceName) })+ })}+ backLabel="Back to Trace"+ onRetry={loadData}+ />+ {:else if response}+ {@const trace = response.aiTrace}++ <Card.Root>+ <Card.Header>+ <Card.Title>Trace Details</Card.Title>+ <Card.Description>Details of this specific AI trace</Card.Description>+ </Card.Header>+ <Card.Content>+ <div class="grid grid-cols-2 gap-4 md:grid-cols-4">+ <LabelValue label="Model" value={trace.model || '-'} mono />+ <LabelValue label="Provider" value={trace.provider || '-'} mono />+ <LabelValue label="Duration" value={formatDuration(trace.duration)} mono large />+ <LabelValue+ label="Recorded At"+ value={formatDateTime(trace.recordedAt, { timezone })}+ mono+ />+ <LabelValue label="Input Tokens" value={trace.inputTokens.toLocaleString()} mono />+ <LabelValue label="Output Tokens" value={trace.outputTokens.toLocaleString()} mono />+ <LabelValue label="Total Tokens" value={trace.totalTokens.toLocaleString()} mono />+ <LabelValue label="Total Cost" value={formatCost(trace.totalCost)} mono large />+ <LabelValue label="Input Cost" value={formatCost(trace.inputCost)} mono />+ <LabelValue label="Output Cost" value={formatCost(trace.outputCost)} mono />+ <LabelValue label="Finish Reason" value={trace.finishReason || '-'} mono />+ <LabelValue label="Operation" value={trace.operation || '-'} mono />+ {#if trace.cachedTokens > 0}+ <LabelValue label="Cached Tokens" value={trace.cachedTokens.toLocaleString()} mono />+ {/if}+ {#if trace.reasoningTokens > 0}+ <LabelValue label="Reasoning Tokens" value={trace.reasoningTokens.toLocaleString()} mono />+ {/if}+ {#if trace.userId}+ <LabelValue label="User ID" value={trace.userId} mono />+ {/if}+ <LabelValue label="Server" value={trace.serverName || '-'} mono />+ <LabelValue label="Version" value={trace.appVersion || '-'} mono />+ </div>+ </Card.Content>+ </Card.Root>++ {#if trace.attributes && Object.keys(trace.attributes).length > 0}+ <Card.Root>+ <Card.Header>+ <Card.Title>Attributes</Card.Title>+ <Card.Description>Additional metadata attached to this trace</Card.Description>+ </Card.Header>+ <Card.Content>+ <AttributesGrid attributes={trace.attributes} />+ </Card.Content>+ </Card.Root>+ {/if}++ {#if response.conversation}+ {@const conv = response.conversation}+ {@const chatMessages = extractMessages(conv.input, conv.output)}++ <Card.Root>+ <Card.Header class="flex flex-row items-center justify-between">+ <div>+ <Card.Title>Conversation</Card.Title>+ <Card.Description>Messages exchanged with the model</Card.Description>+ </div>+ <button+ class="text-xs text-muted-foreground hover:text-foreground transition-colors"+ onclick={() => showRawJson = !showRawJson}+ >+ {showRawJson ? 'Chat view' : 'Raw JSON'}+ </button>+ </Card.Header>+ <Card.Content>+ {#if showRawJson || !chatMessages}+ <div class="space-y-4">+ {#if conv.input}+ <div>+ <p class="mb-2 text-xs font-medium text-muted-foreground uppercase tracking-wide">Input</p>+ <div class="max-h-96 overflow-auto rounded-md bg-muted p-4">+ <pre class="font-mono text-sm whitespace-pre-wrap break-words">{formatConversationContent(conv.input)}</pre>+ </div>+ </div>+ {/if}+ {#if conv.output}+ <div>+ <p class="mb-2 text-xs font-medium text-muted-foreground uppercase tracking-wide">Output</p>+ <div class="max-h-96 overflow-auto rounded-md bg-muted p-4">+ <pre class="font-mono text-sm whitespace-pre-wrap break-words">{formatConversationContent(conv.output)}</pre>+ </div>+ </div>+ {/if}+ </div>+ {:else}+ <div class="space-y-3">+ {#each chatMessages as msg}+ <div class="flex gap-3 {msg.role === 'assistant' ? '' : ''}">+ <div class="flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-xs font-medium+ {msg.role === 'user' ? 'bg-primary text-primary-foreground' :+ msg.role === 'assistant' ? 'bg-muted text-muted-foreground border' :+ msg.role === 'system' ? 'bg-amber-500/15 text-amber-700 dark:text-amber-400' :+ 'bg-muted text-muted-foreground'}">+ {getRoleLabel(msg.role).charAt(0)}+ </div>+ <div class="flex-1 min-w-0">+ <p class="text-xs font-medium text-muted-foreground mb-1">{getRoleLabel(msg.role)}</p>+ {#each getMessageImages(msg.content) as imageUrl}+ <div class="mb-2 max-w-sm">+ <img src={imageUrl} alt="Attached image" class="rounded-md border max-h-64 object-contain" />+ </div>+ {/each}+ {#if getMessageText(msg.content)}+ <div class="rounded-lg px-3 py-2 text-sm whitespace-pre-wrap break-words+ {msg.role === 'user' ? 'bg-primary/10' :+ msg.role === 'system' ? 'bg-amber-500/10 font-mono text-xs' :+ 'bg-muted'}">+ {getMessageText(msg.content)}+ </div>+ {/if}+ </div>+ </div>+ {/each}+ </div>+ {/if}+ </Card.Content>+ </Card.Root>+ {/if}+ {/if}+</div>diff --git a/frontend/src/routes/ai-traces/[traceName]/[traceId]/+page.ts b/frontend/src/routes/ai-traces/[traceName]/[traceId]/+page.tsnew file mode 100644index 00000000..fba1ca02--- /dev/null+++ b/frontend/src/routes/ai-traces/[traceName]/[traceId]/+page.ts@@ -0,0 +1,16 @@+import { redirect } from '@sveltejs/kit';+import { authState } from '$lib/state/auth.svelte';+import type { PageLoad } from './$types';++export const prerender = false;++export const load: PageLoad = ({ params }) => {+ if (!authState.isAuthenticated) {+ throw redirect(302, '/login');+ }++ return {+ traceName: params.traceName,+ traceId: params.traceId+ };+};diff --git a/website/app/page.tsx b/website/app/page.tsxindex 80b98e68..88b6f6ee 100644--- a/website/app/page.tsx+++ b/website/app/page.tsx@@ -22,6 +22,7 @@ import {Lock,Terminal,GitCompare,+ Workflow,} from "lucide-react";import { CodeTabs } from "@/components/code-tabs";import { ImpactScoreVisual } from "@/components/impact-score-visual";@@ -291,6 +292,58 @@ export default function Home() {</div></section>+ {/* Section: AI Tracing */}+ <section className="py-20 bg-zinc-50/50 border-b border-zinc-100">+ <div className="container mx-auto px-4 max-w-5xl">+ <div className="flex flex-col md:flex-row items-center gap-12 lg:gap-20">+ <div className="flex-1 space-y-6">+ <div className="w-12 h-12 bg-violet-50 rounded-2xl flex items-center justify-center">+ <Workflow className="w-6 h-6 text-violet-600" />+ </div>+ <h3 className="text-2xl md:text-3xl font-bold text-zinc-900 tracking-tight">+ AI Tracing for LLM observability+ </h3>+ <p className="text-zinc-600 text-lg leading-relaxed">+ Monitor every AI call with full cost, token usage, latency, and conversation tracking.+ Works with OpenRouter, OpenAI, Anthropic, and any OpenTelemetry-compatible provider — zero code changes needed.+ </p>+ <ul className="space-y-3 pt-2">+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-violet-500"></div>+ Per-call cost and token tracking+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-violet-500"></div>+ Conversation replay with chat view+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-violet-500"></div>+ P50/P95 latency per agent and model+ </li>+ </ul>+ <Link+ href="/product/ai-tracing"+ className="inline-flex items-center text-sm font-medium text-violet-600 hover:text-violet-800 transition-colors"+ >+ Learn more about AI Tracing <ArrowRight className="ml-1 h-4 w-4" />+ </Link>+ </div>+ <div className="flex-1 w-full relative">+ <div className="absolute inset-0 bg-gradient-to-tl from-violet-100/50 to-transparent rounded-3xl transform -rotate-3 scale-105 -z-10"></div>+ <div className="relative rounded-xl overflow-hidden border border-zinc-200 bg-white">+ <Image+ src="/images/ai-traces-detail.png"+ alt="AI Tracing Dashboard"+ width={800}+ height={600}+ className="w-full h-auto"+ />+ </div>+ </div>+ </div>+ </div>+ </section>+{/* Section 7: Open Source & Self-Hosting */}<section className="py-20 bg-white border-b border-zinc-100"><div className="container mx-auto px-4 max-w-5xl">@@ -597,6 +650,17 @@ export default function Home() {filtering and sorting.</AccordionContent></AccordionItem>+ <AccordionItem value="item-14" className="border-b-zinc-200">+ <AccordionTrigger className="text-zinc-900 hover:text-zinc-700 hover:no-underline text-left">+ Does Traceway support AI/LLM observability?+ </AccordionTrigger>+ <AccordionContent className="text-zinc-600 leading-relaxed">+ Yes. Traceway captures AI traces from any provider that exports+ OpenTelemetry spans with <code className="bg-zinc-100 px-1 py-0.5 rounded text-sm">gen_ai.*</code> attributes.+ OpenRouter has built-in support — enable Observability in your settings and add Traceway as a destination.+ Every LLM call is tracked with model, input/output tokens, costs, latency, and the full conversation content.+ </AccordionContent>+ </AccordionItem></Accordion></div></section>diff --git a/website/app/product/ai-tracing/page.tsx b/website/app/product/ai-tracing/page.tsxnew file mode 100644index 00000000..892725de--- /dev/null+++ b/website/app/product/ai-tracing/page.tsx@@ -0,0 +1,250 @@+import Image from "next/image";+import Link from "next/link";++import { Badge } from "@/components/ui/badge";+import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";+import { ArrowRight, DollarSign, MessageSquareText, BarChart3, Shield } from "lucide-react";++export default function AiTracingPage() {+ return (+ <main className="min-h-screen bg-white text-zinc-950 font-sans selection:bg-zinc-100 selection:text-zinc-900">+ {/* Hero Section */}+ <section className="relative pt-16 pb-20 overflow-hidden">+ <div className="absolute inset-0 -z-10 h-full w-full bg-white bg-[radial-gradient(#e5e7eb_1px,transparent_1px)] [background-size:16px_16px] [mask-image:radial-gradient(ellipse_50%_50%_at_50%_50%,#000_70%,transparent_100%)]"></div>+ <div className="container mx-auto px-4 text-center">+ <Badge variant="secondary" className="mb-4 bg-violet-50 text-violet-700 hover:bg-violet-100 px-2.5 py-0.5 border border-violet-100 text-xs font-normal rounded-full">+ AI Tracing+ </Badge>+ <h1 className="text-4xl md:text-6xl font-bold tracking-tight mb-6 text-zinc-900">+ See every AI call, <br /> <span className="text-transparent bg-clip-text bg-gradient-to-r from-violet-600 to-blue-600">its cost, and its conversation</span>+ </h1>+ <p className="text-zinc-600 text-lg md:text-xl max-w-2xl mx-auto mb-10 leading-relaxed font-medium">+ Monitor LLM costs, token usage, latency, and conversations across every AI provider. Works with OpenRouter, OpenAI, Anthropic, and any OpenTelemetry-compatible provider.+ </p>+ <div className="flex flex-col sm:flex-row items-center justify-center gap-3">+ <Link href="https://docs.tracewayapp.com/client/openrouter" className="inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium transition-all cursor-pointer h-10 px-6 bg-zinc-900 text-white hover:bg-zinc-800 shadow-lg shadow-zinc-900/20">+ Get Started <ArrowRight className="ml-2 h-4 w-4" />+ </Link>+ <Link href="http://cloud.tracewayapp.com/register" className="inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium transition-all cursor-pointer h-10 px-6 border border-zinc-200 bg-white hover:bg-zinc-50 text-zinc-900 shadow-sm">+ Try Traceway Cloud+ </Link>+ </div>+ </div>+ </section>++ {/* Cost Visibility Section */}+ <section className="py-24 bg-white border-y border-zinc-100">+ <div className="container mx-auto px-4 max-w-5xl">+ <div className="flex flex-col md:flex-row items-center gap-12 lg:gap-20">+ <div className="flex-1 space-y-6">+ <div className="w-12 h-12 bg-green-50 rounded-2xl flex items-center justify-center">+ <DollarSign className="w-6 h-6 text-green-600" />+ </div>+ <h2 className="text-2xl md:text-3xl font-bold text-zinc-900 tracking-tight">Know exactly what AI costs you</h2>+ <p className="text-zinc-600 text-lg leading-relaxed">+ Every AI call is tracked with input cost, output cost, and total cost. See cost breakdowns per agent, per model, and per time period.+ Spot cost spikes before they hit your invoice.+ </p>+ <ul className="space-y-3 pt-2">+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-green-500"></div>+ Per-call cost tracking with input/output breakdown+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-green-500"></div>+ Aggregated cost per agent and model+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-green-500"></div>+ Token usage with cached and reasoning token tracking+ </li>+ </ul>+ </div>+ <div className="flex-1 w-full relative">+ <div className="absolute inset-0 bg-gradient-to-tr from-green-100/50 to-transparent rounded-3xl transform rotate-3 scale-105 -z-10"></div>+ <div className="relative rounded-xl overflow-hidden border border-zinc-200 bg-white">+ <Image+ src="/images/ai-traces-cost.png"+ alt="AI Traces Cost Dashboard"+ width={800}+ height={600}+ className="w-full h-auto"+ />+ </div>+ </div>+ </div>+ </div>+ </section>++ {/* Conversation Replay Section */}+ <section className="py-24 bg-zinc-50/50">+ <div className="container mx-auto px-4 max-w-5xl">+ <div className="flex flex-col md:flex-row-reverse items-center gap-12 lg:gap-20">+ <div className="flex-1 space-y-6">+ <div className="w-12 h-12 bg-violet-50 rounded-2xl flex items-center justify-center">+ <MessageSquareText className="w-6 h-6 text-violet-600" />+ </div>+ <h2 className="text-2xl md:text-3xl font-bold text-zinc-900 tracking-tight">Replay every conversation</h2>+ <p className="text-zinc-600 text-lg leading-relaxed">+ See the exact prompt sent to the model and the full response it generated. Debug unexpected model behavior, catch hallucinations, and understand what your AI agents are actually doing.+ </p>+ <ul className="space-y-3 pt-2">+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-violet-500"></div>+ Full prompt and completion stored and rendered as chat+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-violet-500"></div>+ Raw JSON view for debugging edge cases+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-violet-500"></div>+ Privacy mode available to exclude conversation content+ </li>+ </ul>+ </div>+ <div className="flex-1 w-full relative">+ <div className="absolute inset-0 bg-gradient-to-tl from-violet-100/50 to-transparent rounded-3xl transform -rotate-3 scale-105 -z-10"></div>+ <div className="relative rounded-xl overflow-hidden border border-zinc-200 bg-white">+ <Image+ src="/images/ai-traces-conversation.png"+ alt="AI Trace Conversation View"+ width={800}+ height={600}+ className="w-full h-auto"+ />+ </div>+ </div>+ </div>+ </div>+ </section>++ {/* Performance & Latency Section */}+ <section className="py-24 bg-white border-y border-zinc-100">+ <div className="container mx-auto px-4 max-w-5xl">+ <div className="flex flex-col md:flex-row items-center gap-12 lg:gap-20">+ <div className="flex-1 space-y-6">+ <div className="w-12 h-12 bg-blue-50 rounded-2xl flex items-center justify-center">+ <BarChart3 className="w-6 h-6 text-blue-600" />+ </div>+ <h2 className="text-2xl md:text-3xl font-bold text-zinc-900 tracking-tight">Understand AI latency at every percentile</h2>+ <p className="text-zinc-600 text-lg leading-relaxed">+ AI calls have wildly variable latency. See P50 and P95 duration breakdowns per agent, identify which models or providers are causing slowdowns, and track performance over time.+ </p>+ <ul className="space-y-3 pt-2">+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-blue-500"></div>+ P50/P95 latency per agent and model+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-blue-500"></div>+ Drill down from agent to individual calls+ </li>+ <li className="flex items-center gap-3 text-zinc-700">+ <div className="w-1.5 h-1.5 rounded-full bg-blue-500"></div>+ Throughput and token usage trends+ </li>+ </ul>+ </div>+ <div className="flex-1 w-full relative">+ <div className="absolute inset-0 bg-gradient-to-tr from-blue-100/50 to-transparent rounded-3xl transform rotate-3 scale-105 -z-10"></div>+ <div className="relative rounded-xl overflow-hidden border border-zinc-200 bg-white">+ <Image+ src="/images/ai-traces-latency.png"+ alt="AI Traces Performance Dashboard"+ width={800}+ height={600}+ className="w-full h-auto"+ />+ </div>+ </div>+ </div>+ </div>+ </section>++ {/* Zero Code Section */}+ <section className="py-24 bg-zinc-50/50">+ <div className="container mx-auto px-4 max-w-5xl">+ <div className="flex flex-col items-center text-center space-y-6">+ <div className="w-12 h-12 bg-amber-50 rounded-2xl flex items-center justify-center">+ <Shield className="w-6 h-6 text-amber-600" />+ </div>+ <h2 className="text-2xl md:text-3xl font-bold text-zinc-900 tracking-tight">Zero code changes required</h2>+ <p className="text-zinc-600 text-lg leading-relaxed max-w-2xl">+ If you're using OpenRouter, enable Observability in your settings and point it at Traceway. That's it.+ For other providers, any OpenTelemetry-instrumented AI call with <code className="bg-zinc-100 px-1.5 py-0.5 rounded text-sm">gen_ai.*</code> attributes is automatically captured.+ </p>+ <div className="flex flex-wrap items-center justify-center gap-8 md:gap-10 pt-6">+ <Image src="/images/frameworks/openrouter.png" alt="OpenRouter" width={40} height={40} className="h-8 w-auto opacity-80 hover:opacity-100 transition-all duration-200" />+ <Image src="/images/frameworks/otel.png" alt="OpenTelemetry" width={40} height={40} className="h-8 w-auto opacity-80 hover:opacity-100 transition-all duration-200" />+ </div>+ </div>+ </div>+ </section>++ {/* FAQ Section */}+ <section className="py-24 bg-zinc-50 border-t border-zinc-100">+ <div className="container mx-auto px-4 max-w-3xl">+ <div className="text-center mb-12">+ <h2 className="text-3xl font-bold mb-4 text-zinc-900 tracking-tight">Frequently Asked Questions</h2>+ <p className="text-zinc-600 text-lg">+ Common questions about AI tracing with Traceway.+ </p>+ </div>++ <Accordion type="single" collapsible className="w-full">+ <AccordionItem value="item-1" className="border-b-zinc-200">+ <AccordionTrigger className="text-zinc-900 hover:text-zinc-700 hover:no-underline text-left">+ Why do I need AI observability?+ </AccordionTrigger>+ <AccordionContent className="text-zinc-600 leading-relaxed">+ AI API calls are expensive and unpredictable. A single prompt with a large context window can cost 100x more than average.+ Without observability, cost spikes go unnoticed until the invoice arrives. AI tracing gives you per-call visibility into costs,+ token usage, latency, and the actual conversations happening between your app and AI models.+ </AccordionContent>+ </AccordionItem>+ <AccordionItem value="item-2" className="border-b-zinc-200">+ <AccordionTrigger className="text-zinc-900 hover:text-zinc-700 hover:no-underline text-left">+ Which AI providers are supported?+ </AccordionTrigger>+ <AccordionContent className="text-zinc-600 leading-relaxed">+ Any provider or gateway that exports OTLP traces with <code className="bg-zinc-100 px-1 py-0.5 rounded text-sm">gen_ai.*</code> semantic convention attributes.+ OpenRouter has built-in support — just enable Observability in your settings. For other providers, use any OpenTelemetry SDK+ to instrument your AI calls and send them to Traceway.+ </AccordionContent>+ </AccordionItem>+ <AccordionItem value="item-3" className="border-b-zinc-200">+ <AccordionTrigger className="text-zinc-900 hover:text-zinc-700 hover:no-underline text-left">+ Is the conversation content stored securely?+ </AccordionTrigger>+ <AccordionContent className="text-zinc-600 leading-relaxed">+ Conversation content (prompts and completions) is stored separately from trace metadata in object storage (S3 or local filesystem).+ If you're self-hosting, the data never leaves your infrastructure. OpenRouter also offers a Privacy Mode that excludes+ conversation content entirely, sending only metadata like model, tokens, and costs.+ </AccordionContent>+ </AccordionItem>+ <AccordionItem value="item-4" className="border-b-zinc-200">+ <AccordionTrigger className="text-zinc-900 hover:text-zinc-700 hover:no-underline text-left">+ How does this work with OpenRouter?+ </AccordionTrigger>+ <AccordionContent className="text-zinc-600 leading-relaxed">+ OpenRouter has a built-in Observability feature that broadcasts OTLP traces for every LLM call.+ You add Traceway as an “OpenTelemetry Collector” destination in your OpenRouter settings with your Traceway endpoint+ and project token. No code changes needed — OpenRouter handles the instrumentation automatically.+ </AccordionContent>+ </AccordionItem>+ <AccordionItem value="item-5" className="border-b-zinc-200">+ <AccordionTrigger className="text-zinc-900 hover:text-zinc-700 hover:no-underline text-left">+ Can I track costs across multiple models?+ </AccordionTrigger>+ <AccordionContent className="text-zinc-600 leading-relaxed">+ Yes. AI traces capture the specific model used for each call (e.g., <code className="bg-zinc-100 px-1 py-0.5 rounded text-sm">openai/gpt-4-turbo</code>, <code className="bg-zinc-100 px-1 py-0.5 rounded text-sm">anthropic/claude-3-opus</code>).+ You can see cost breakdowns per model, compare token efficiency across providers, and identify which models give you the best value.+ </AccordionContent>+ </AccordionItem>+ </Accordion>+ </div>+ </section>+ </main>+ );+}diff --git a/website/components/framework-marquee.tsx b/website/components/framework-marquee.tsxindex 64c1c955..925228a8 100644--- a/website/components/framework-marquee.tsx+++ b/website/components/framework-marquee.tsx@@ -17,6 +17,7 @@ const frameworks = [{ name: "Cloudflare", src: "/images/frameworks/cloudflare.png" },{ name: "jQuery", src: "/images/frameworks/jquery.png" },{ name: "Symfony", src: "/images/frameworks/symfony.png" },+ { name: "OpenRouter", src: "/images/frameworks/openrouter.png" },];export function FrameworkMarquee() {diff --git a/website/components/home-tabs.tsx b/website/components/home-tabs.tsxindex 9b1f1434..4b12a193 100644--- a/website/components/home-tabs.tsx+++ b/website/components/home-tabs.tsx@@ -2,7 +2,7 @@import { useState } from "react";import Link from "next/link";-import { Server, Monitor, Network, ArrowRight } from "lucide-react";+import { Server, Monitor, Network, Workflow, ArrowRight } from "lucide-react";const tabs = [{@@ -59,6 +59,24 @@ const tabs = [],href: "/product/distributed-tracing",},+ {+ id: "ai-agents",+ label: "AI Agents",+ icon: Workflow,+ iconBg: "bg-violet-50",+ iconColor: "text-violet-600",+ dotColor: "bg-violet-500",+ heading: "Track every AI call, its cost, and its conversation.",+ description:+ "Monitor LLM costs, token usage, and latency across every provider. See the full prompt and completion for every call, with per-agent and per-model breakdowns.",+ bullets: [+ "Per-call cost and token tracking",+ "Conversation replay with chat view",+ "P50/P95 latency per agent and model",+ "Works with OpenRouter and any OTel provider",+ ],+ href: "/product/ai-tracing",+ },];export function HomeTabs() {diff --git a/website/components/mobile-nav.tsx b/website/components/mobile-nav.tsxindex 713e1152..801031a4 100644--- a/website/components/mobile-nav.tsx+++ b/website/components/mobile-nav.tsx@@ -73,6 +73,13 @@ export function MobileNav() {>Distributed Tracing</Link>+ <Link+ href="/product/ai-tracing"+ className="text-lg font-medium text-zinc-600 hover:text-zinc-900 transition-colors pl-2"+ onClick={() => setIsOpen(false)}+ >+ AI Tracing+ </Link><div className="border-t border-zinc-100"></div><Linkhref="/cloud"diff --git a/website/components/site-header.tsx b/website/components/site-header.tsxindex 64e4b4e3..ca60f2a5 100644--- a/website/components/site-header.tsx+++ b/website/components/site-header.tsx@@ -4,7 +4,7 @@ import { useState, useRef, useEffect } from "react";import Link from "next/link";import Image from "next/image";-import { Github, AlertCircle, Activity, Video, ChevronDown, Network } from "lucide-react";+import { Github, AlertCircle, Activity, Video, ChevronDown, Network, Workflow } from "lucide-react";import { MobileNav } from "@/components/mobile-nav";export function SiteHeader() {@@ -105,6 +105,19 @@ export function SiteHeader() {<p className="text-xs text-zinc-500 mt-0.5">Cross-service traces linked to frontend session replays</p></div></Link>+ <Link+ href="/product/ai-tracing"+ className="flex items-start gap-3 rounded-md p-3 hover:bg-zinc-50 transition-colors"+ onClick={() => setOpen(false)}+ >+ <div className="w-8 h-8 bg-violet-50 rounded-lg flex items-center justify-center shrink-0 mt-0.5">+ <Workflow className="w-4 h-4 text-violet-600" />+ </div>+ <div>+ <div className="text-sm font-medium text-zinc-900">AI Tracing</div>+ <p className="text-xs text-zinc-500 mt-0.5">Monitor LLM costs, tokens, latency, and conversations</p>+ </div>+ </Link></div>)}</div>diff --git a/website/public/images/ai-traces-conversation.png b/website/public/images/ai-traces-conversation.pngnew file mode 100644index 00000000..81d6435cBinary files /dev/null and b/website/public/images/ai-traces-conversation.png differdiff --git a/website/public/images/ai-traces-cost.png b/website/public/images/ai-traces-cost.pngnew file mode 100644index 00000000..8988ae9bBinary files /dev/null and b/website/public/images/ai-traces-cost.png differdiff --git a/website/public/images/ai-traces-detail.png b/website/public/images/ai-traces-detail.pngnew file mode 100644index 00000000..521ab9c9Binary files /dev/null and b/website/public/images/ai-traces-detail.png differdiff --git a/website/public/images/ai-traces-instances.png b/website/public/images/ai-traces-instances.pngnew file mode 100644index 00000000..bbf62794Binary files /dev/null and b/website/public/images/ai-traces-instances.png differdiff --git a/website/public/images/ai-traces-latency.png b/website/public/images/ai-traces-latency.pngnew file mode 100644index 00000000..76ef7692Binary files /dev/null and b/website/public/images/ai-traces-latency.png differdiff --git a/website/public/images/ai-traces-list.png b/website/public/images/ai-traces-list.pngnew file mode 100644index 00000000..871db200Binary files /dev/null and b/website/public/images/ai-traces-list.png differdiff --git a/website/public/images/frameworks/openrouter.png b/website/public/images/frameworks/openrouter.pngnew file mode 100644index 00000000..48f4a9a1Binary files /dev/null and b/website/public/images/frameworks/openrouter.png differ