Deploying with HTTP Cloud Functions
HTTP Cloud Functions are the fastest way to deploy an Agent. They support auto-scaling and pay-as-you-go billing, making them ideal for lightweight Agents and low-frequency invocation scenarios.
Prerequisites
- A CloudBase environment has been activated
- Node.js 18+ is installed
- Agent development is complete
Quick Deployment
1. Project Structure
my-agent/
├── index.js # Entry file
├── agent.js # Agent logic
└── package.json # Dependency configuration
2. Agent Logic
// agent.js
const { createAgent: createLangchainAgent } = require("langchain");
const { MemorySaver } = require("@langchain/langgraph");
const { ChatOpenAI } = require("@langchain/openai");
const { clientTools } = require("@cloudbase/agent-adapter-langchain");
const checkpointer = new MemorySaver();
function createLcAgent() {
// Use the built-in large model endpoint from CloudBase
const model = new ChatOpenAI({
model: process.env.TCB_AI_MODEL || "hunyuan-turbos-latest",
apiKey: process.env.TCB_API_KEY,
configuration: {
baseURL: `https://${process.env.TCB_ENV_ID}.api.tcloudbasegateway.com/v1/ai/hunyuan/v1`,
},
});
return createLangchainAgent({
model,
checkpointer,
middleware: [clientTools()],
});
}
module.exports = { createLcAgent };
3. Entry File
// index.js
const { LangchainAgent } = require("@cloudbase/agent-adapter-langchain");
const { createExpressRoutes } = require("@cloudbase/agent-server");
const { createLcAgent } = require("./agent");
const express = require("express");
function createAgent() {
const lcAgent = createLcAgent();
return {
agent: new LangchainAgent({ agent: lcAgent }),
};
}
const app = express();
createExpressRoutes({ createAgent, express: app });
// Cloud function entry point
exports.main = app;
4. Dependency Configuration
{
"name": "my-agent",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@cloudbase/agent-adapter-langchain": "latest",
"@cloudbase/agent-server": "latest",
"langchain": "latest",
"@langchain/openai": "latest",
"@langchain/langgraph": "latest",
"express": "latest"
}
}
5. Deployment
Method 1: Deploy with CLI
Prerequisites: Install and log in to the CloudBase CLI
# Install CLI
npm install -g @cloudbase/cli
# Log in
tcb login
Deploy command:
# Run from the project directory
tcb fn deploy <functionName> --httpFn -e <env-id>
# Check deployment status
tcb fn list -e <env-id>
- Fast deployment — no manual packaging or uploading required
- Supports automated command-line deployment
- Can be integrated into CI/CD pipelines
- Detailed deployment logs and error messages
For more CLI commands and options, refer to the CloudBase CLI Documentation.
Method 2: Deploy via Console
Upload a code package through the CloudBase console:
- Go to the CloudBase console
- Select Cloud Functions
- Click Create Cloud Function
- Select the HTTP Function type
- Upload the code package or edit the code online
Configuration Reference
Environment Variables
Set environment variables in the cloud function configuration:
| Variable | Description | Example |
|---|---|---|
TCB_ENV_ID | CloudBase environment ID | your-env-id |
TCB_API_KEY | CloudBase API key | ak-xxx |
TCB_AI_MODEL | Model name | hunyuan-turbos-latest |
TCB_AI_PROVIDER | Model provider (optional) | hunyuan or deepseek |
CloudBase has built-in support for Tencent Hunyuan and DeepSeek large models — no external API key required. For the list of supported models, refer to the Model Configuration Guide.
Timeout Configuration
Agents typically require longer execution times. It is recommended to set a generous timeout:
- Recommended timeout: 60–120 seconds
- Maximum timeout: 900 seconds (15 minutes)
Memory Configuration
Configure memory based on Agent complexity:
| Scenario | Recommended Memory |
|---|---|
| Simple conversation | 256 MB |
| With tool calls | 512 MB |
| Complex workflows | 1024 MB+ |
Access Configuration
After deployment, there are two ways to access the Agent service:
Method 1: Access via AI Agent Integration (Recommended)
Create an AI Agent in the CloudBase console, associate it with the deployed cloud function, and obtain a unified access URL.
Steps:
- Go to the CloudBase console
- Select AI
- Click Create Agent
- Select Existing Service
- Choose the deployed HTTP cloud function
- Complete the creation
Access URL:
https://<env-id>.api.tcloudbasegateway.com/v1/aibot/bots/<agent-name>/send-message
Example:
curl 'https://<env-id>.api.tcloudbasegateway.com/v1/aibot/bots/<agent-name>/send-message' \
-H 'Content-Type: application/json' \
-H 'Accept: text/event-stream' \
--data-raw '{
"threadId": "550e8400-e29b-41d4-a716-446655440000",
"messages": [
{ "id": "msg-1", "role": "user", "content": "Hello" }
],
"tools": [],
"context": [],
"state": {},
"forwardedProps": {}
}'
Advantages:
- ✅ Unified access URL format
- ✅ Built-in authentication and access control
- ✅ Supports Agent UI visual debugging
- ✅ Automatic monitoring and logging integration
Method 2: Configure HTTP Access
Directly configure an HTTP access path for the cloud function, suitable for custom access requirements.
Steps:
- Go to the CloudBase console
- Select Cloud Functions → HTTP Access
- Click Create
- Select the cloud function and configure the path
- Set the authentication method
Access URL:
https://<env-id>.<region>.app.tcloudbase.com/<path>
Example:
https://my-env-xxx.ap-shanghai.app.tcloudbase.com/agent
Authentication Configuration:
| Authentication Method | Description | Use Case |
|---|---|---|
| No authentication | Public access | Test environments |
| CloudBase auth | Requires login state | Production environments |
It is strongly recommended to enable authentication in production environments to prevent API abuse. Method 1 (AI Agent integration) is recommended as it has a more comprehensive built-in security mechanism.
Local Debugging
Before deploying, it is recommended to develop and debug locally. For detailed guidance, refer to:
The local development guide covers:
- Getting the project code (GitHub template / syncing from an online service)
- Installing dependencies and configuring environment variables
- Starting the local service
- Debugging with cURL or a proxy
- Troubleshooting common issues
Monitoring & Logging
Viewing Logs
View function logs through the CloudBase console:
- Go to the CloudBase console
- Select Cloud Functions → Logs
- Select the corresponding function to view its logs
Performance Monitoring
View in the console:
- Invocation count
- Average duration
- Error rate
- Memory usage
FAQ
1. Timeout Error
Problem: Function execution times out
Solution:
- Increase the timeout configuration
- Optimize Agent logic to reduce unnecessary API calls
- Consider deploying with CloudBase Run
2. Slow Cold Start
Problem: Slow response on the first call
Solution:
- Reduce the size of dependency packages
- Use provisioned concurrency
- Optimize initialization logic
3. Out of Memory
Problem: OOM error
Solution:
- Increase the memory configuration
- Optimize memory usage
- Avoid loading large models
Best Practices
- Use environment variables: Configure sensitive information via environment variables
- Set a reasonable timeout: Set the timeout based on Agent complexity
- Enable authentication: Authentication must be enabled in production environments
- Monitor logs: Regularly review logs and performance metrics
- Error handling: Implement comprehensive error handling logic