创建Agent
更新时间:2026-11-24
接口说明
- 本接口用于创建一个新的 Agent 定义(Agent Def)。创建成功后返回平台分配的资源 ID,后续创建会话、查询详情等操作均基于该 ID。
- 同一账号下 metadata.name 已存在时返回 409 AgentDefAlreadyExists,不覆盖既有定义。如需修改请调用「更新Agent」接口。
- 声明 spec.network.crossVpc 的 Agent 为异步创建:响应 status=creating,需轮询「查询Agent详情」直到 status=created 后才能创建会话;创建失败时 status=failed,需删除后重建。
- metadata.name 是稳定业务键,创建后不可修改。
- spec.harnessConfig 的结构随 harnessType 变化,省略时按空对象处理,工具权限全部拒绝。
- Metadata、Spec、Model、Network、HarnessConfig 等结构的完整字段定义见「附录」。
请求结构
Plain Text
1POST /v1/agents/create HTTP/1.1
2Host: aw-harness.bj.baidubce.com
3Authorization: authorization string
4
5{
6 "metadata": {
7 "name": "my-agent",
8 "description": "示例 Agent"
9 },
10 "spec": {
11 "harnessType": "opencode",
12 "harnessVersion": "1.18.29",
13 "model": {
14 "modelId": "deepseek-v4.1-flash",
15 "params": {
16 "temperature": 0.7,
17 "topP": 0.9
18 }
19 },
20 "systemPrompt": "你是 {{name}},请帮助用户完成任务。",
21 "skills": ["registry://nsk-example/skill-example@1.0.1"],
22 "setupScript": "export FOO=bar",
23 "capabilities": ["docx", "pdf"],
24 "limits": {
25 "maxLifetime": "2h",
26 "idleTimeout": "30m"
27 },
28 "network": {
29 "allowInternetAccess": false,
30 "allowOut": ["203.0.113.0/24", "*.example.com"],
31 "denyOut": ["198.51.100.0/24"]
32 },
33 "memorySpaces": [
34 {
35 "memorySpaceId": "ms-xxxx",
36 "accessMode": "read_write"
37 }
38 ],
39 "mcpServers": ["mcp-xxxx"],
40 "harnessConfig": {
41 "tools": {
42 "builtin": ["bash", "file_ops"],
43 "deny": ["web_fetch"],
44 "bash": {
45 "go test *": "allow",
46 "rm -rf *": "deny"
47 }
48 }
49 }
50 }
51}
请求头域
除公共头域外,无其它特殊头域。
请求参数
| 参数名称 | 类型 | 是否必需 | 参数位置 | 描述 |
|---|---|---|---|---|
| metadata | Metadata | 是 | RequestBody参数 | Agent 元数据。 |
| spec | Spec | 是 | RequestBody参数 | Agent 业务定义。 |
返回头域
除公共头域,无其它特殊头域。
返回参数
| 参数名称 | 类型 | 描述 |
|---|---|---|
| agentId | String | 平台分配的资源 ID,ag- 前缀。 |
| agentName | String | Agent 名称。 |
| version | Long | 配置版本号,服务端自增。 |
| harnessType | String | 内核类型。 |
| status | String | Agent 状态:creating、created、failed、deleting。 |
错误码
| 错误码 | 错误描述 | HTTP状态码 | 中文解释 |
|---|---|---|---|
| InvalidArgument | The request parameters are invalid. | 400 | 请求参数不合法,如技能引用格式错误、工具配置冲突、自定义网关缺少凭据、capabilities 取值非法或与内核冲突、文档技能缺 bash、bash 规则语法与内核不符、请求体含未识别字段。字段错误可通过 details[].field 定位。 |
| Forbidden | Operation requires an admin caller. | 403 | 缺少鉴权账号、跨账号访问他人资源,或携带 Actor 头调用仅限管理员的写接口。 |
| AgentDefQuotaExceeded | Agent def quota exceeded. | 403 | Agent 数量已达配额上限,需删除不用的 Agent 或申请调高上限。 |
| CrossVPCAgentDefQuotaExceeded | Cross-VPC agent def quota exceeded. | 403 | 跨 VPC Agent 数量已达独立配额上限。该配额与总配额分别生效。 |
| MemorySpaceQuotaExceeded | Memory space quota exceeded. | 403 | Agent 绑定的记忆空间数超限。 |
| KnowledgeDisabled | Knowledge capability is not enabled in this region. | 403 | 企业 RAG 检索在当前地域不可用;或声明了 knowledge 能力但该地域不支持。 |
| AgentDefAlreadyExists | Agent def already exists. | 409 | 创建的目标名字下已有 Agent,不覆盖。 |
| ModelServiceNotFound | Model service not found. | 404 | 引用的模型服务不存在。 |
| McpNotFound | MCP not found. | 404 | 引用的 MCP 不存在。 |
| MemorySpaceNotFound | Memory space not found. | 404 | 引用的记忆空间不存在或不属于本账号。 |
| MemorySpaceNotReady | Memory space is not ready. | 409 | 记忆空间处于 CREATING、FAILED 或 DELETING,此刻不可绑定。 |
| PersistFailed | Failed to persist the agent def. | 500 | 服务端存储读写失败,可重试。 |
| Internal | Internal server error. | 500 | 服务端内部错误,保留 requestId 联系维护方。 |
请求示例
Plain Text
1POST http://aw-harness.bj.baidubce.com/v1/agents/create
2Host: aw-harness.bj.baidubce.com
3Authorization: bce-auth-v1/f81d3b34e48048fbb2634dc7882d7e21/2015-08-11T04:17:29Z/3600/host/74c506f68c65e26c633bfa104c863fffac5190fdec1ec24b7c03eb5d67d2e1de
4Content-Type: application/json
5
6{
7 "metadata": {
8 "name": "my-agent",
9 "description": "示例 Agent"
10 },
11 "spec": {
12 "harnessType": "opencode",
13 "model": {
14 "modelId": "deepseek-v4.1-flash"
15 },
16 "systemPrompt": "你是 {{name}},请帮助用户完成任务。",
17 "harnessConfig": {
18 "tools": {
19 "builtin": ["bash", "file_ops"],
20 "bash": {
21 "go test *": "allow",
22 "go build *": "allow"
23 }
24 }
25 }
26 }
27}
返回示例
Plain Text
1HTTP/1.1 200 OK
2Content-Type: application/json;charset=UTF-8
3x-bce-request-id: 9f3d0e6a-1c2b-4d5e-8a7f-0b1c2d3e4f5a
4Server: BWS
5
6{
7 "result": {
8 "agentId": "ag-7f3c9b2e",
9 "agentName": "my-agent",
10 "version": 1,
11 "harnessType": "opencode",
12 "status": "created"
13 },
14 "requestId": "9f3d0e6a-1c2b-4d5e-8a7f-0b1c2d3e4f5a"
15}
评价此篇文章
