AI agents are autonomous systems that perceive their environment, make decisions, and take actions to achieve goals. Unlike simple chatbots, agents can use tools, maintain memory across sessions, and execute multi-step workflows. This guide shows you how to build production-ready agents using Chinese AI models through TokenEase.
What Makes an AI Agent?
An AI agent differs from a standard LLM application in four key dimensions:
- Autonomy: The agent decides what to do next without human intervention at every step
- Tool Use: It can call APIs, query databases, send emails, and interact with external systems
- Memory: It remembers past interactions, learned facts, and user preferences
- Planning: It breaks complex goals into sub-tasks and executes them sequentially
Agent Architecture
1. Perception Layer — Receives user input, parses intent, extracts entities
2. Memory System — Short-term (conversation) + Long-term (vector DB) + Episodic (past sessions)
3. Planning Engine — Task decomposition, prioritization, dependency management
4. Tool Registry — Available functions the agent can invoke
5. Action Executor — Runs tool calls, handles errors, manages state
6. Reflection Loop — Evaluates results, learns from failures, adjusts strategy
Building Your First Agent
Here is a minimal but complete agent implementation using TokenEase and Chinese models:
Step 1: Agent Class Structure
import json, requests
from typing import List, Dict, Any
class ChineseAIAgent:
def __init__(self, api_key: str, model: str = "deepseek"):
self.api_key = api_key
self.model = model
self.base_url = "https://tokenease.io/v1"
self.memory = [] # Conversation history
self.tools = []
self.state = {} # Agent working memory
def register_tool(self, name: str, func, description: str, parameters: dict):
self.tools.append({
"type": "function",
"function": {
"name": name,
"description": description,
"parameters": parameters
}
})
self._tool_registry = getattr(self, '_tool_registry', {})
self._tool_registry[name] = func
def think(self, user_input: str) -> Dict[str, Any]:
self.memory.append({"role": "user", "content": user_input})
response = requests.post(
f"{self.base_url}/chat/completions",
headers={"Authorization": f"Bearer {self.api_key}"},
json={
"model": self.model,
"messages": self.memory,
"tools": self.tools,
"tool_choice": "auto"
}
)
return response.json()["choices"][0]["message"]
def act(self, tool_calls: List[Dict]) -> List[Dict]:
results = []
for call in tool_calls:
name = call["function"]["name"]
args = json.loads(call["function"]["arguments"])
result = self._tool_registry[name](**args)
results.append({
"tool_call_id": call["id"],
"role": "tool",
"content": json.dumps(result)
})
return results
Step 2: Register Tools
agent = ChineseAIAgent(api_key="your-tokenease-key", model="deepseek")
# Tool 1: Web search
agent.register_tool(
name="web_search",
func=lambda query: search_engine_api(query),
description="Search the web for current information",
parameters={
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"]
}
)
# Tool 2: Calculator
agent.register_tool(
name="calculate",
func=lambda expression: {"result": eval(expression)},
description="Evaluate mathematical expressions",
parameters={
"type": "object",
"properties": {
"expression": {"type": "string", "description": "Math expression to evaluate"}
},
"required": ["expression"]
}
)
# Tool 3: Save to memory
agent.register_tool(
name="remember",
func=lambda fact: agent._save_long_term(fact),
description="Save a fact to long-term memory",
parameters={
"type": "object",
"properties": {
"fact": {"type": "string", "description": "Fact to remember"}
},
"required": ["fact"]
}
)
Step 3: Run the Agent Loop
def run_agent(agent: ChineseAIAgent, task: str, max_turns: int = 10):
for turn in range(max_turns):
# Think: what should I do?
thought = agent.think(task if turn == 0 else "Continue")
if thought.get("content"):
print(f"Agent: {thought['content']}")
# Check if done
if not thought.get("tool_calls"):
break
# Act: execute tools
tool_results = agent.act(thought["tool_calls"])
agent.memory.extend(tool_results)
print(f"Turn {turn+1}: Executed {len(tool_results)} tool(s)")
return agent.memory
# Run it
result = run_agent(agent, "Search for Bitcoin price, calculate 10% of it, and remember the result.")
Memory Strategies
Memory separates toys from production agents. Here are three levels:
Level 1: Conversation Buffer
Simply keep the full message history. Works for short tasks but hits context limits quickly.
Level 2: Summarized Memory
def summarize_memory(self, messages: List[Dict]) -> str:
summary_prompt = "Summarize the key facts and decisions from this conversation in 3 sentences."
response = requests.post(
f"{self.base_url}/chat/completions",
headers={"Authorization": f"Bearer {self.api_key}"},
json={
"model": "deepseek",
"messages": messages + [{"role": "user", "content": summary_prompt}]
}
)
return response.json()["choices"][0]["message"]["content"]
Level 3: Vector Memory (RAG)
from sentence_transformers import SentenceTransformer
import numpy as np
class VectorMemory:
def __init__(self):
self.encoder = SentenceTransformer('BAAI/bge-large-zh-v1.5')
self.facts = []
self.embeddings = []
def add(self, fact: str):
self.facts.append(fact)
emb = self.encoder.encode(fact)
self.embeddings.append(emb)
def retrieve(self, query: str, top_k: int = 3) -> List[str]:
query_emb = self.encoder.encode(query)
scores = [np.dot(query_emb, e) for e in self.embeddings]
top_indices = np.argsort(scores)[-top_k:][::-1]
return [self.facts[i] for i in top_indices]
Model Selection for Agents
| Model | Agent Strength | Best For |
|---|---|---|
| deepseek DeepSeek-V4 | Reasoning | Complex planning, multi-step reasoning |
| zhipu GLM-5.1 | Tool Accuracy | Chinese-language tool interactions |
| qwen Qwen-Plus | Speed | High-frequency agent loops, real-time |
| kimi Kimi-K3 | Context | Agents with massive history or documents |
Agent Patterns
ReAct Pattern (Reason + Act)
The most popular agent pattern. The model alternates between reasoning and acting:
# ReAct loop
def react_loop(agent, task, max_steps=5):
for step in range(max_steps):
# Reason
reasoning = agent.think(f"Task: {task}\nWhat should I do next?")
# Act
if reasoning.get("tool_calls"):
results = agent.act(reasoning["tool_calls"])
agent.memory.extend(results)
else:
return reasoning["content"]
return "Max steps reached"
Multi-Agent Swarm
For complex tasks, deploy multiple specialized agents:
class AgentSwarm:
def __init__(self):
self.researcher = ChineseAIAgent(model="deepseek") # Deep reasoning
self.writer = ChineseAIAgent(model="zhipu") # Chinese writing
self.critic = ChineseAIAgent(model="deepseek") # Quality check
def execute(self, task: str) -> str:
# Research phase
facts = self.researcher.think(f"Research: {task}")
# Writing phase
draft = self.writer.think(f"Write based on: {facts['content']}")
# Review phase
review = self.critic.think(f"Critique this draft: {draft['content']}")
# Revise if needed
if "issues" in review["content"].lower():
draft = self.writer.think(f"Revise based on feedback: {review['content']}")
return draft["content"]
Deployment Patterns
Pattern 1: Synchronous API
Simple HTTP endpoint. Client sends task, waits for completion. Best for simple agents with few tool calls.
Pattern 2: Async with WebSockets
Real-time streaming of agent thoughts and actions. Users see the agent "thinking" in real-time.
Pattern 3: Background Workers
Task queued → Worker processes → Results stored → User polls or gets notified. Best for long-running agents.
Error Handling for Robust Agents
class ResilientAgent(ChineseAIAgent):
def act_with_retry(self, tool_calls, max_retries=2):
for call in tool_calls:
for attempt in range(max_retries + 1):
try:
result = self._execute_single(call)
break
except Exception as e:
if attempt == max_retries:
result = {"error": str(e), "suggestion": "Try a different approach"}
else:
# Ask model to fix the call
fix = self.think(f"Tool call failed: {e}. How should I adjust?")
call = fix.get("tool_calls", [call])[0]
yield result
Security Considerations
- Input validation: Never pass user input directly to tool arguments without sanitization
- Tool sandboxing: Run tools in isolated environments with limited permissions
- Rate limiting: Prevent agents from making excessive API calls
- Human-in-the-loop: Require approval for destructive actions (deletions, payments)
- Audit logging: Log every tool call and decision for debugging
Conclusion
Building AI agents with Chinese models is now as straightforward as with any other LLM. The key ingredients are: a clear architecture, robust memory, well-designed tools, and error handling. With TokenEase, you can prototype with one model and productionize with another — all through the same API.
Start simple: a single-agent ReAct loop with 2-3 tools. Add vector memory when conversations grow. Scale to multi-agent swarms when tasks become complex. The Chinese AI ecosystem has the models you need at a fraction of the cost.
Build Your First AI Agent
Get $1 free API credit to prototype agents with all 6 Chinese AI models.
Start Building