AI Agents

How to Build a DeepSeek Agent: From API Key to Your First Agent

Step-by-step guide to building a DeepSeek Agent with V4 Pro and V4 Flash. Covers API setup, model selection, tool calling, the agent loop, thinking mode, and production error handling — with Python and Node.js examples.

Judy2 min read
How to Build a DeepSeek Agent: From API Key to Your First Agent

Information checked October 13, 2026. Verify current model names, prices, and API behavior before running the code.

A DeepSeek agent needs an API credential, a model, a bounded tool set, and a loop that returns tool results to the model. The demonstration is small; production work adds permissions, validation, stop conditions, idempotency, logs, and review.

1. Create the credential

Create a key in the DeepSeek Platform and store it in an environment variable or secret manager. Do not commit it. Funding and account rules can change, so follow the console rather than a copied minimum-deposit claim.

2. Choose the current model

DeepSeek’s live catalog lists deepseek-flash as production V4.1 Flash with 1M context, up to 384K output, vision, tool calls, JSON output, Responses API, and Anthropic-format access.

Published V4.1 Flash prices per million tokens are $0.003/$0.006 cache-hit input, $0.15/$0.30 cache-miss input, and $0.60/$1.20 output for off-peak/peak periods. The catalog also displays deepseek-v4-pro as V4-Pro-0813, but DeepSeek’s release notice says Pro-alias traffic temporarily routes to V4.1 Flash while V4.1-Pro is pending. Use deepseek-flash in a first build and log the served model.

3. Implement the bounded loop

The documented tool-calling pattern is:

  1. send messages plus JSON-schema tool definitions;
  2. inspect returned tool_calls;
  3. validate the function name and arguments;
  4. authorize and execute the tool in your application;
  5. append the matching tool result;
  6. call the model again until it returns a final answer or reaches a stop condition.

In pseudocode:

messages = [{"role": "user", "content": task}]
for turn in range(MAX_TURNS):
    response = client.chat.completions.create(
        model="deepseek-flash", messages=messages, tools=tools
    )
    message = response.choices[0].message
    messages.append(message)
    if not message.tool_calls:
        return message.content
    for call in message.tool_calls:
        args = validate(call.function.name, call.function.arguments)
        result = execute_with_policy(call.function.name, args)
        messages.append({"role":"tool","tool_call_id":call.id,"content":result})
raise RuntimeError("turn limit reached")

The loop is not safe merely because it terminates. Separate read and write tools, use least-privilege credentials, and require approval for destructive, financial, external-message, or access-control actions. Retried writes need idempotency keys.

4. Treat thinking fields as protocol data

Thinking mode and fields such as reasoning_content must follow the current DeepSeek protocol. They may help protocol debugging, but they are not a reliable explanation of a decision and should not be used as audit evidence. Audit the observable inputs, tool requests, validations, approvals, results, and final action instead.

5. Test before adding tools

Test malformed arguments, missing evidence, duplicate events, timeouts, denied approvals, and downstream outages. Limit turns, parallel calls, elapsed time, and spend. Start sequentially; add concurrency only for independent, read-only work.

For schema and executor details, see DeepSeek Agent function calling. For the broader choice between a custom loop and an existing client, see what a DeepSeek Agent is and the build-versus-buy framework.

The production milestone is not “the model called a tool.” It is “the system stopped safely, produced an observable result, and handled failure without an unauthorized side effect.”

Related posts

https://floatboat.ai/blog/how-to-build-deepseek-agent

Frequently Asked Questions

What are the minimum parts of a DeepSeek Agent?
You need a credential, current model, bounded tools, loop, validation, stop conditions, and logs.
Which model should a first build use?
Start with deepseek-flash and escalate only after evaluation.
Is changing the base URL enough?
Not for production. Feature compatibility and error behavior must be tested.
What is the agent loop?
Send tools, receive calls, execute safely, return results, and repeat until a bounded stop.
How do I prevent infinite loops?
Set turn, time, call, and spend limits.
How should writes be protected?
Use least privilege, validation, approval, and idempotency keys.
What should tests include?
Test missing evidence, malformed arguments, duplicates, outages, and denied approvals.
When should I use an existing harness?
Use one when it already provides the sandbox, approvals, and session controls you need.