Back to blogs
AI & LLMOpenAIGeminiAINode.js

Building AI Chatbots with LLM APIs in Backend Systems

A backend engineer's guide to integrating OpenAI and Gemini APIs — covering streaming, context management, rate limiting, cost control, and production patterns.

Doni Putra PurbawaDoni Putra Purbawa
December 5, 20239 min read

LLM APIs have changed how we build product features. This post covers the backend patterns I've learned from integrating OpenAI and Gemini into production systems.

Streaming Responses

For good UX, stream tokens to the client as they arrive. In Node.js with Express:

app.post('/api/chat', async (req, res) => {
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');

const stream = await openai.chat.completions.create({ model: 'gpt-4o', messages: req.body.messages, stream: true, });

for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content || ''; if (content) { res.write(`data: ${JSON.stringify({ content })}

`); } }

res.end(); });

Context Window Management

Managing the conversation history to stay within token limits is critical. I use a sliding window approach that preserves the system prompt and recent messages.

Cost Control

LLM APIs are pay-per-token. Production cost control requires: input/output token counting per request, per-user rate limits, caching for repeated queries, and model tiering (use GPT-4o-mini for simple tasks).

The biggest win is always caching — many user queries are semantically similar.

Doni Putra Purbawa

Doni Putra Purbawa

Cloud Architect & Senior Backend Engineer | AWS, Fintech, Cloud & AI

Designing reliable AWS cloud architecture, fintech platforms, and AI-powered backend systems. Based in Indonesia, open to Japan relocation.

View full profile →

Comments

No comments yet. Be the first to share your thoughts!

Leave a Comment

Comments are moderated and will appear after approval.