惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
雷峰网
雷峰网
博客园_首页
WordPress大学
WordPress大学
博客园 - 司徒正美
爱范儿
爱范儿
博客园 - 聂微东
IT之家
IT之家
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
博客园 - Franky
V
V2EX
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Wiring DeepSeek Into Your NestJS Stack: A Backend Walkthr...
rarenode · 2026-06-12 · via DEV Community

I gotta say, wiring DeepSeek Into Your NestJS Stack: A Backend Walkthrough

Last quarter I got pulled into a sprint that had nothing to do with LLMs. The product team wanted "AI summaries" on a dashboard I'd already shipped months ago, and the manager who'd blocked the feature left for a competitor. So there I was, the unfortunate backend engineer holding the bag at 2pm on a Tuesday, staring at a Jira ticket that said "add AI" like that meant anything.

I tried the usual suspects. I burned an afternoon reading pricing pages until my eyes glazed over. Then I stumbled onto Global API and their unified access to 184 models, with prices ranging from $0.01 to $3.50 per million tokens. I ended up routing everything through DeepSeek for the heavy lifting, and I haven't looked back since. Fwiw, this is the kind of integration that should take a week but can realistically be done in a single afternoon if you know where to cut corners.

Here's how I wired DeepSeek into a NestJS backend without losing my mind, and the numbers that convinced me to keep it there.

Why DeepSeek, And Why Bother Comparing Anything

Let me be blunt: most AI integration blog posts are vendor propaganda. They show you three lines of code, a screenshot of a happy dashboard, and call it a day. The thing nobody tells you is that the model you pick will quietly eat your runway over six months. A difference of two dollars per million tokens compounds into a real problem when you're serving 50 million requests a month.

So before I wrote a single line of TypeScript, I made a spreadsheet. Here's the relevant subset, sorted by what actually matters to a backend engineer: dollars going out, and whether the context window fits your use case.

Model Input ($/M) Output ($/M) Context
DeepSeek V4 Flash 0.27 1.10 128K
DeepSeek V4 Pro 0.55 2.20 200K
Qwen3-32B 0.30 1.20 32K
GLM-4 Plus 0.20 0.80 128K
GPT-4o 2.50 10.00 128K

Look at that GPT-4o row. It's not bad technology. It's bad economics. $10.00 per million output tokens is a price tag designed for a quarter where you don't have to explain your AWS bill. For a startup doing summarization, classification, or extraction work, it's the wrong tool.

DeepSeek V4 Flash at $0.27 input and $1.10 output is, imo, the sweet spot. The 128K context is enough for most document workflows. The V4 Pro at 200K context is what I reach for when someone dumps a 150-page PDF at the API and expects coherent output. Both options sit in that magical 40-65% cost reduction band versus the "default" everyone reaches for first.

(Aside: if you've ever wondered why your CFO keeps asking about "the AI line item," this table is your answer. Print it out. Slide it across the table. Watch the room go quiet.)

The NestJS Part Nobody Warned Me About

NestJS is opinionated. That's usually a good thing, until you try to integrate something that doesn't ship with a @nestjs/llm package. Spoiler: there isn't one. You're going to roll your own service, and you're going to do it the way NestJS wants you to: dependency injection, providers, modules, the whole ceremony.

Here's the module structure I landed on, after about three iterations of refactoring:

// src/llm/llm.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { LlmService } from './llm.service';
import { LlmController } from './llm.controller';

@Module({
  imports: [ConfigModule.forRoot()],
  providers: [LlmService],
  controllers: [LlmController],
  exports: [LlmService],
})
export class LlmModule {}

That's boilerplate, but it matters. NestJS's DI container is one of the few things in the Node ecosystem that doesn't make me want to throw my laptop. Lean into it.

The service itself is where the real work lives. I use the OpenAI SDK pointed at Global API's base URL, because the request/response shape is identical and that SDK has been battle-tested by about a million other people:

// src/llm/llm.service.ts
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import OpenAI from 'openai';
import type { ChatCompletionMessageParam } from 'openai/resources/chat';

@Injectable()
export class LlmService {
  private readonly logger = new Logger(LlmService.name);
  private readonly client: OpenAI;

  constructor(private readonly config: ConfigService) {
    this.client = new OpenAI({
      apiKey: this.config.getOrThrow<string>('GLOBAL_API_KEY'),
      baseURL: 'https://global-apis.com/v1',
    });
  }

  async summarize(content: string): Promise<string> {
    const messages: ChatCompletionMessageParam[] = [
      {
        role: 'system',
        content: 'You are a concise summarizer. Output three bullet points.',
      },
      { role: 'user', content },
    ];

    const response = await this.client.chat.completions.create({
      model: 'deepseek-ai/DeepSeek-V4-Flash',
      messages,
      temperature: 0.3,
    });

    return response.choices[0]?.message?.content ?? '';
  }

  async *streamSummary(content: string): AsyncIterable<string> {
    const stream = await this.client.chat.completions.create({
      model: 'deepseek-ai/DeepSeek-V4-Flash',
      stream: true,
      messages: [
        { role: 'system', content: 'You are a concise summarizer.' },
        { role: 'user', content },
      ],
    });

    for await (const chunk of stream) {
      const delta = chunk.choices[0]?.delta?.content;
      if (delta) yield delta;
    }
  }
}

Two methods on purpose. The non-streaming one is fine for batch jobs, webhook handlers, and cron tasks — anywhere you can wait a beat. The async generator is what you want for HTTP responses going back to a user. They feel the difference between 1.2s of dead air and the first token landing in 200ms. Same cost. Wildly different UX.

Under the hood, both calls go through Global API's unified SDK layer, which means you can swap deepseek-ai/DeepSeek-V4-Flash for gpt-4o or qwen3-32b with literally one string change. I tested this during a vendor review meeting by hot-swapping models mid-call. The room was impressed. I was mostly relieved it didn't crash.

Production-Grade Concerns (The Boring Part That Matters)

The code above is the demo version. It works on your laptop. It does not survive contact with real traffic. Here's what I added in week two, after the first latency spike at 3am.

1. Caching, But Not The Naive Kind

If your prompt contains a system message and a user message, and the user message is "summarize this document," then the same document from the same user is going to produce the same summary. Hash the input, store the output, set a TTL of 24 hours. A 40% hit rate is realistic for most production workloads, and that translates directly to 40% less money leaving your bank account.

I use Redis for this, but anything with a TTL will do. Don't over-engineer it.

2. Stream Or Die

I already mentioned streaming in the service code. Let me reinforce it: if you're sending a completion back over HTTP, stream it. Server-Sent Events work great with NestJS and barely add any code. The user perceives the response as faster even when total time-to-completion is identical. That's not me being a UX purist — it's measurable in our engagement metrics.

3. Pick The Right Tier For The Job

DeepSeek V4 Flash is my default. But for "what's the sentiment of this one-sentence review?" I don't need 128K of context. I need cheap. Global API exposes a GA-Economy tier for exactly this kind of work, and you can land at roughly 50% cost reduction on simple queries by routing them there. Build a router in your service that classifies the request complexity and picks the model accordingly. I do this with a simple heuristic (prompt length + a keyword check) and it works fine 95% of the time.

4. Monitor Quality Like You Mean It

This is the part where most teams screw up. They measure cost. They measure latency. They forget to measure whether the output is any good. Track user satisfaction scores, thumbs-up rates, whatever signal you have. We log a sample of completions and have a human spot-check 50 per week. It's tedious. It catches model regressions faster than any automated eval suite I've ever seen.

5. Fallback Because Rate Limits Are Real

Even with 184 models at your disposal, you will hit rate limits. It's not a question of if, it's when. The right move is a fallback chain: try DeepSeek V4 Flash, on 429 try DeepSeek V4 Pro, on 429 try Qwen3-32B. The user gets a response. Your error budget stays intact. Graceful degradation is the difference between a five-minute incident and a PagerDuty weekend.

// src/llm/llm.service.ts (excerpt)
private async callWithFallback(
  messages: ChatCompletionMessageParam[],
): Promise<string> {
  const chain = [
    'deepseek-ai/DeepSeek-V4-Flash',
    'deepseek-ai/DeepSeek-V4-Pro',
    'Qwen3-32B',
  ];

  let lastError: unknown;
  for (const model of chain) {
    try {
      const res = await this.client.chat.completions.create({
        model,
        messages,
      });
      return res.choices[0]?.message?.content ?? '';
    } catch (err) {
      this.logger.warn(`Model ${model} failed: ${(err as Error).message}`);
      lastError = err;
    }
  }
  throw lastError;
}

This is a primitive version. In production, I'd add jitter on the retries, a circuit breaker (look up RFC's take on Hystrix patterns if you want the academic version), and per-model timeouts. But the shape is right.

The Numbers, Because Engineering Is A Budget Exercise

After running DeepSeek V4 Flash for eight weeks in production, here's what we measured:

  • Average latency: 1.2s end-to-end for non-streamed calls, 200ms time-to-first-token for streamed calls
  • Throughput: 320 tokens/second sustained, which is well above our actual demand
  • Quality: 84.6% average benchmark score across our internal eval suite
  • Cost: roughly 40-65% cheaper than the GPT-4o baseline, depending on whether the workload is input-heavy or output-heavy

The latency number is honest. It's not a vendor benchmark on a single isolated request — it's a p50 from our actual production logs. Throughput is similarly measured, not promised. I bring this up because I've read too many blog posts where "1.2s average latency" turns out to mean "1.2s when the cache is warm and the moon is in the right phase."

The quality score, 84.6%, comes from a custom eval suite we built around our actual use cases. Generic benchmarks like MMLU are useful for picking between models in the abstract, but they don't tell you whether a model is good at summarizing your specific kind of document. Build your own. It's not that hard, and the signal is dramatically better.

What I'd Do Differently

A few things, in case you're about to do this yourself:

  • Start with Global API's free credits. They give you 100 free credits to test all 184 models, which is more than enough to run a real workload for a few days. Use it to find the model that's actually right for your data, not the one with the best marketing page.
  • Don't build a vendor abstraction layer on day one. I know, "clean architecture" and all that. The reality is that every LLM provider has a slightly different quirk, and you'll spend more time on the abstraction than on the actual integration. Get the call working. Refactor when the second vendor joins. Not before.
  • Token counting is the unsexy skill you need. Estimate your input and output tokens before each call. It's the only way to forecast your bill with any accuracy. A rough rule of thumb: 1 token ≈ 4 characters of English text. Multiply by your call volume, multiply by your per-million-token rate, and you'll have a number you can actually defend in a budget meeting.
  • Set a hard cost ceiling in your monitoring. If your daily spend crosses a threshold, page someone. The blast radius of a runaway loop calling an LLM is real, and it's measured in thousands of dollars per hour if you don't catch it.

The 10-Minute Setup (For Real This Time)

I promised the setup was under 10 minutes. Here's the actual sequence, in order:

  1. npm i @nestjs/config openai
  2. Set GLOBAL_API_KEY in your .env
  3. Copy the LlmModule, LlmService, and `L