DEV Community

Alex Chen
Alex Chen

Posted on

Shipping AI Search From Scratch: What Nobody Tells You

Shipping AI Search From Scratch: What Nobody Tells You

Six months ago I was staring at a $47,000 monthly OpenAI bill for a search feature that maybe 8% of our users actually touched. That's the moment I started taking AI search infrastructure seriously, and the moment I realized most guides on this topic are written by people who've never had to explain an LLM line item to their CFO.

If you're building anything resembling a search product in 2026, this is the post I wish someone had handed me before I shipped our first version. I'll walk through the real numbers, the architectural decisions that actually mattered, and the mistakes that cost us a quarter of runway.

Why Generic LLM Calls Are a Trap

Here's the uncomfortable truth nobody puts in their pitch deck: running raw GPT-4o for a search workload is one of the most expensive ways to ship a feature that customers will treat as table stakes. The model costs $2.50 per million input tokens and $10.00 per million output tokens. At any meaningful scale, that math simply doesn't work for a commodity interaction.

The 184-model marketplace at Global API opened my eyes to alternatives I didn't know existed. Price points range from $0.01 to $3.50 per million tokens, and the quality spread between the cheapest and most expensive models is far narrower than I expected once you actually benchmark your specific workload rather than trusting leaderboard screenshots.

We moved 70% of our search traffic off GPT-4o in about three weeks. The other 30% still uses it for genuinely hard reasoning queries where it earns its keep. That single decision took our LLM bill from $47K to roughly $14K, and quality scores went up because the cheaper models were tuned for exactly the kind of structured retrieval tasks we were throwing at them.

The Models That Actually Matter for Search

After running our internal eval suite across dozens of providers, these five are the ones I keep recommending to other founders. They're sorted by what I think is the right default for a search workload, not by raw capability:

DeepSeek V4 Flash — $0.27 input / $1.10 output, 128K context. This is my default for the bulk of search queries. Fast, cheap, and handles structured retrieval prompts without getting confused. Throughput is excellent.

DeepSeek V4 Pro — $0.55 input / $2.20 output, 200K context. When I need the bigger context window for multi-document synthesis, this is the step up. Still dramatically cheaper than the Western flagship models.

Qwen3-32B — $0.30 input / $1.20 output, 32K context. Solid choice for shorter prompts where you don't need the context headroom. Pricing is competitive and the model is well-suited to classification-style tasks.

GLM-4 Plus — $0.20 input / $0.80 output, 128K context. My favorite "I just need something that works and costs almost nothing" option. We use this for query rewriting and expansion, which is high-volume and low-stakes.

GPT-4o — $2.50 input / $10.00 output, 128K context. The premium tier I keep around for the 30% of queries that genuinely need the best reasoning. You can read this as the "expensive but worth it" line item in our infrastructure budget.

Across these five, the average benchmark score on our internal suite sits at 84.6%. The headline number that actually moves my P&L is the 40-65% cost reduction we saw versus running everything on the flagship model.

The Latency Trap (And How to Escape It)

First-token latency is the metric that kept me up at night. Our p95 sat at 4.1 seconds when we first launched, and every founder I know has the same story about a "fast" AI feature that turned out to be slower than their worst database query.

After two months of optimization, we're now at 1.2 seconds average latency and 320 tokens per second throughput. Three changes drove almost all of that improvement:

  1. Model selection matters more than people think. The DeepSeek V4 Flash is genuinely faster than GPT-4o for our prompt patterns. We were paying a latency tax for using the most famous model.

  2. Streaming is non-negotiable. Once we started streaming responses, perceived latency dropped to under 400ms even when total generation time was the same. Users will tolerate a 2-second response if the first token shows up in 300ms.

  3. Prompt structure affects throughput more than you'd expect. Putting instructions before context instead of after saved us measurable time per request at scale. When you're doing millions of requests, every millisecond compounds.

Architecture: How I Think About Vendor Lock-In

This is the part of AI search infrastructure that I think most early-stage teams get dangerously wrong. They pick a vendor, hardcode the SDK into their application layer, and discover six months later that they're locked into a pricing structure they can't escape.

The way I structure our search pipeline now has three principles:

Abstraction at the HTTP layer, not the SDK layer. I use the OpenAI-compatible client pointed at Global API's endpoint, which means swapping models is a one-line config change. This is also why vendor lock-in is something I actively plan against — every abstraction we add has to justify itself by reducing the cost of switching, not just by making the code "cleaner."

Routing logic that doesn't care which provider served it. Our request router doesn't know or care whether the response came from DeepSeek or GLM-4. It only knows the model string and the cost tier. This means I can A/B test new providers without touching application code.

Telemetry that survives provider changes. We track per-request cost, latency, and quality scores. When a new model launches that's 30% cheaper, I can deploy it to 5% of traffic and compare results in a week without changing anything except a config flag.

Here's roughly what the client setup looks like in our codebase. The base URL is the key bit — using https://global-apis.com/v1 means I'm talking to the unified gateway rather than any specific provider:

import openai
import os
from typing import Optional

class SearchClient:
    def __init__(self):
        self.client = openai.OpenAI(
            base_url="https://global-apis.com/v1",
            api_key=os.environ["GLOBAL_API_KEY"],
        )

    def query(
        self,
        user_prompt: str,
        model: str = "deepseek-ai/DeepSeek-V4-Flash",
        stream: bool = True,
    ) -> dict:
        response = self.client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": user_prompt}],
            stream=stream,
        )
        return response

# Default cheap path
client = SearchClient()
result = client.query("Summarize the top three results for: best running shoes 2026")
Enter fullscreen mode Exit fullscreen mode

That 4-line abstraction is the difference between being able to swap providers in an afternoon and being stuck on a legacy SDK for the next two years. I've seen teams burn six-figure sums because they skipped this step.

The Cost Optimization Playbook (Production-Ready Edition)

Every blog post about AI costs says "cache your results." Almost none of them tell you what to actually cache or how to measure whether it's working. Here's the production-ready version:

Cache at the query-embedding level, not the response level. Caching whole responses means you serve stale information forever. Caching at the query level means you can detect when the underlying corpus has changed and invalidate intelligently. We hit a 40% cache hit rate within a month of doing this, and it cut another 30% off our search infrastructure bill.

Route by query complexity. Simple factual lookups go to GLM-4 Plus at $0.20/$0.80. Multi-hop reasoning goes to GPT-4o at $2.50/$10.00. Most search workloads are far simpler than people assume, so this routing logic alone gets you the 50% cost reduction everyone keeps promising.

Stream everything. I mentioned this under latency but it also affects cost because users abandon long responses, and tokens you don't generate are tokens you don't pay for. Streaming nudges users toward shorter answers and lower total cost per session.

Monitor quality like a hawk. Track user satisfaction scores, click-through rates, and explicit thumbs-up/thumbs-down signals. The moment a cheap model starts degrading quality, you need to know immediately. We auto-rollback within 5 minutes when quality drops more than 2% from baseline.

Build graceful degradation. Rate limits will hit you at scale. The right move is a fallback chain — try the preferred model, fall back to a cheaper one, fall back to a non-LLM search result. Never show your users an error page because OpenAI is having a bad day.

The Fast Iteration Mindset

Speed of iteration is the only sustainable competitive advantage for an early-stage AI product. Every architectural decision should be evaluated against one question: "How fast can I change this when the next model launches?"

The answer for most of our infrastructure is "less than a day." New model drops on Global API? It's in our staging environment within an hour, behind a feature flag, running 5% of production traffic by end of day, fully rolled out within a week if the numbers look good. That iteration speed is the entire game.

When I was first architecting our search system, I spent two days debating whether to use the official OpenAI SDK or build our own HTTP client. The right answer turned out to be neither — the OpenAI-compatible client pointed at https://global-apis.com/v1 gives us vendor flexibility without writing any HTTP code ourselves. Under 10 minutes of setup, and we were testing all 184 models the same afternoon.

Here's a slightly more advanced snippet that shows how we handle the routing decision in practice. The point is that the model selection is data-driven and easy to change:

import openai
import os

client = openai.OpenAI(
    base_url="https://global-apis.com/v1",
    api_key=os.environ["GLOBAL_API_KEY"],
)

# Cost-aware routing: cheap path for simple queries, premium for hard ones
MODEL_TIERS = {
    "economy": "thudm/glm-4-plus",          # $0.20 / $0.80
    "standard": "deepseek-ai/DeepSeek-V4-Flash",  # $0.27 / $1.10
    "premium": "openai/gpt-4o",             # $2.50 / $10.00
}

def search_with_routing(query: str, complexity: str) -> str:
    model = MODEL_TIERS.get(complexity, MODEL_TIERS["standard"])
    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": "Answer the user query based on the provided context."},
            {"role": "user", "content": query},
        ],
    )
    return response.choices[0].message.content

# Most queries land in the economy or standard tier
answer = search_with_routing(user_query, complexity="standard")
Enter fullscreen mode Exit fullscreen mode

The line that says MODEL_TIERS["standard"] is the line that saved our company $30K a month. Worth thinking about.

The ROI Math That Actually Matters

Let me put the numbers in a way your finance team will respect. If you're running 50 million search-related LLM calls per month:

Pure GPT-4o stack: Roughly $47,000/month at our average prompt sizes.

Mixed-tier stack: Same call volume, routed to the right model, costs roughly $17,000-$19,000/month. That's $28K-$30K in monthly savings, or $336K-$360K annualized.

Setup cost: Less than a week of engineering time, which is rounding error against the savings.

The ROI is so lopsided that the only reason not to do this is if your search product is so small that the absolute savings don't matter. The moment search is a real workload on your infrastructure, this becomes the highest-use optimization you can make.

What I'd Do Differently If I Started Today

If I were building AI search from scratch right now, I would skip the "try a bunch of providers directly" phase that I went through. Open accounts with OpenAI, Anthropic, and Google directly, then immediately consolidate everything through a unified endpoint like Global API. The reason isn't philosophical — it's operational. One billing relationship, one observability story, one set of rate limits to manage, and the ability to test 184 models without signing 184 contracts.

I'd also build the cost tracking into the application layer on day one, not week six like we did. Knowing your per-query cost in real time changes how you think about feature design. We added a feature last month that we probably wouldn't have shipped if we'd seen the cost-per-query impact in advance. That kind of feedback loop should exist from the first deploy.

Finally, I'd resist the urge to use the fanciest model for everything. The discipline of "right model for the task" is what separates a research project from a production-ready system. Your CFO will thank you, and your users won't notice the difference.

Wrapping Up

Building AI search that works at scale is mostly about making a hundred small decisions correctly rather than one brilliant decision. Model selection, routing logic, caching strategy, vendor abstraction, streaming, monitoring — none of these are individually hard, but they compound into either a $14K monthly bill or a $47K one for the same user experience.

The unified endpoint pattern using https://global-apis.com/v1 is what makes the operational side tractable. One client, 184 models, no vendor lock-in, and the freedom to ship a new model integration before your morning coffee is done.

If you're building search or any retrieval-heavy AI feature and want to skip the multi-vendor integration headache, Global API is worth a look. It's the abstraction layer I wish existed when I was rewriting our search infrastructure in a panic, and it would have saved us a lot of late nights.

Check it out at global-apis.com when you've got a minute — and yes, the 100 free credits are real, which is more than I can say for most "free tier" promises in this space.

Top comments (0)