← Back to Blog
·6 min read

Building an AI Cost Estimator: Parsing LLM Outputs into Interactive UI Components

Learn how to build an interactive B2B cost estimator. We show how to parse unstructured user input into typed JSON using LLM Structured Outputs, and bind it to React sliders, charts, and accordions.

AI IntegrationReactZodStructured OutputsUI ComponentsWeb Development

In B2B and service-oriented industries, getting a price estimate is the most critical step of a customer journey. However, traditional cost estimators are often tedious, requiring users to fill out complex forms with dozens of checkboxes, dropdowns, and text fields. This friction leads to low completion rates. What if users could simply describe their requirements in plain text, and have the system instantly generate a detailed, interactive estimate?

This is the power of an AI Cost Estimator. By using Large Language Models (LLMs), we can take unstructured natural language inputs and parse them into structured, reliable data structures. In this article, we will walk through the frontend and backend architecture required to make this work, focusing on LLM Structured Outputs, schema validation with Zod, and binding the parsed data to React UI controls.

1. Backend: Enforcing Strict JSON with LLM Structured Outputs

The greatest challenge in AI-powered tools is reliability. If you simply prompt an LLM to "return JSON," it may occasionally output text wraps, invalid formats, or change key names. In production, this breaks your frontend parsing logic.

To solve this, modern APIs (like OpenAI, Gemini, and Anthropic) offer Structured Outputs. This feature forces the model to respond strictly according to a JSON Schema. We define this schema using Zod, a TypeScript-first schema declaration and validation library. The LLM acts as an extraction engine, mapping the user's free-form description directly into our defined structure.

2. Defining the Estimator Zod Schema

For a construction or renovation estimate, we describe a schema that captures the project dimensions (e.g. area, number of rooms), specific requirements (wiring replacement, demolition), and a list of estimated cost items. Here is a TypeScript example of how we define the target JSON structure:

First, we define `EstimateSchema` containing basic parameters like `area` and `rooms`, boolean flags for `demolition` and `rewiring`, and an array `estimateItems` where each item has a `task`, `category`, and `baseCost`. We attach descriptions to fields (e.g., `describe("Area in square meters")`) to help the LLM understand what data should be extracted into each property.

3. Connecting AI Data to React UI State

Once the backend parses the input and returns the validated JSON object, the frontend application loads it into React state. This creates a highly responsive, interactive flow:

  • Initial Load: The user submits their text description. We show a loading indicator. Once the API returns the JSON, we populate the local component state with the AI extraction values.
  • Formula-Based Calculations: While the AI provides the initial estimate items and baseline prices, the final cost math is computed locally using React formulas. If the user changes variables, the cost updates instantly without sending requests back to the LLM.
  • Hybrid Control System: The UI renders controls (sliders, toggles, and dropdowns) that are bound directly to the state. This allows the user to correct the AI. If the AI missed that they need wiring replacement, the user simply turns on the "Rewiring" toggle switch.

4. Rendering Interactive Visual Widgets

To deliver a high-quality experience, the interface should transform raw numbers into visually appealing charts and easy-to-use widgets:

  • Interactive Sliders: Map the `area` parameter to a custom HTML range slider. Dragging the slider increases the area multiplier, dynamically scaling the labor and materials cost of each line item.
  • Dynamic Charts: Group items by category (e.g. Demolition, Finishes, Electrical) and display them in a dynamic SVG circle chart or progress bars. This visually communicates budget breakdown at a glance.
  • Editable Accordions: Present the estimate items grouped by category inside collapsible accordion lists. Allow the user to adjust the unit price, remove redundant tasks, or add custom notes to individual items.

5. Technical Implementation Blueprint

The frontend setup consists of a text area for description input, followed by the estimator dashboard. We manage the state of variables like `area` and `wiring` using React hook states. In the render function, we calculate the totals: we multiply base costs by the current area slider value, adding flat rates for demolition if selected. A summary panel displays the recalculated sum alongside interactive sliders.

By combining the flexible context extraction of LLMs with the strict constraints and fast feedback loop of React and TypeScript, businesses can create automated B2B quote engines that feel smart, fast, and completely reliable.

Ready to discuss your project?

I'm a senior web engineer specializing in React and Next.js — available for freelance projects worldwide.

Location

Kyiv, Ukraine