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

推荐订阅源

WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
小众软件
小众软件
Recent Announcements
Recent Announcements
博客园 - 三生石上(FineUI控件)
Security Archives - TechRepublic
Security Archives - TechRepublic
W
WeLiveSecurity
Cloudbric
Cloudbric
博客园 - 司徒正美
美团技术团队
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
PCI Perspectives
PCI Perspectives
宝玉的分享
宝玉的分享
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
Last Week in AI
Last Week in AI
S
Schneier on Security
N
News | PayPal Newsroom
B
Blog RSS Feed
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
S
Secure Thoughts
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tenable Blog
S
Securelist
L
LangChain Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
I
InfoQ
H
Heimdal Security Blog
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
K
Kaspersky official blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
C
Cisco Blogs

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 Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Wiring DeepSeek Into Your NestJS Stack: A Backend Walkthrough
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