Skip to main content
User feedback helps you understand how well your application performs in production. Braintrust lets you capture feedback and attach it to specific traces for analysis and evaluation.

Types of feedback

Braintrust supports four types of user feedback:
  • Scores: Numeric ratings like thumbs up/down (1 or 0) or relevance scores (0 to 1)
  • Expected values: Corrections that show what the correct output should have been
  • Comments: Free-form text providing additional context
  • Metadata: Structured information like user ID or session ID

Log feedback

Use logFeedback() to attach feedback to a span by its ID. Return the span ID from your application endpoint so users can reference it when submitting feedback.
import { initLogger, wrapTraced } from "braintrust";

const logger = initLogger({ projectName: "My Project" });

// Return span ID from your endpoint
export async function POST(req: Request) {
  return logger.traced(async (span) => {
    const text = await req.text();
    const result = await processRequest(text);
    span.log({ input: text, output: result });

    return {
      result,
      requestId: span.id, // Return this to the client
    };
  });
}

// Log feedback from a separate endpoint
export async function POSTFeedback(req: Request) {
  const body = await req.json();

  logger.logFeedback({
    id: body.requestId, // Span ID from the original request
    scores: {
      correctness: body.score, // 1 for thumbs up, 0 for thumbs down
    },
    comment: body.comment,
    metadata: {
      user_id: body.userId,
    },
  });
}
As you log feedback, the fields update in real time in the Braintrust dashboard.

Collect multiple scores

When multiple users provide feedback on the same span, create child spans for each submission instead of overwriting scores. Braintrust automatically averages the scores in parent spans.
import { initLogger, currentSpan } from "braintrust";

const logger = initLogger({ projectName: "My Project" });

export async function POSTFeedback(req: Request) {
  const body = await req.json();

  // Create a child span for each feedback submission
  await logger.traced(
    async (span) => {
      span.log({
        scores: {
          correctness: body.score,
        },
        comment: body.comment,
        metadata: {
          user_id: body.userId,
          timestamp: new Date().toISOString(),
        },
      });
    },
    {
      name: "user_feedback",
      parent: body.requestId, // Link to original span
    },
  );
}
This pattern preserves all individual feedback while providing aggregated scores at the parent level.

Next steps