import OpenAI from "openai";
const openai = new OpenAI();
const WORKSPACE_ID = "ws_123";
const ARG_API_KEY = "YOUR_API_KEY";
const ARG_BASE = "https://api.arg.ai/api";
// Define arg.ai workspace tools
const tools: OpenAI.ChatCompletionTool[] = [
{
type: "function",
function: {
name: "run_bash",
description: "Run a shell command in the workspace sandbox",
parameters: {
type: "object",
properties: { command: { type: "string" } },
required: ["command"],
},
},
},
{
type: "function",
function: {
name: "read_file",
description: "Read a file from the workspace",
parameters: {
type: "object",
properties: {
path: { type: "string" },
offset: { type: "integer" },
limit: { type: "integer" },
},
required: ["path"],
},
},
},
{
type: "function",
function: {
name: "write_file",
description: "Write a file to the workspace",
parameters: {
type: "object",
properties: {
path: { type: "string" },
content: { type: "string" },
},
required: ["path", "content"],
},
},
},
{
type: "function",
function: {
name: "edit_file",
description: "Replace a string in a file",
parameters: {
type: "object",
properties: {
path: { type: "string" },
old_string: { type: "string" },
new_string: { type: "string" },
},
required: ["path", "old_string", "new_string"],
},
},
},
{
type: "function",
function: {
name: "grep",
description: "Search file contents with a regex pattern",
parameters: {
type: "object",
properties: {
pattern: { type: "string" },
path: { type: "string" },
include: { type: "string" },
},
required: ["pattern"],
},
},
},
];
// Map tool names to arg.ai endpoints
const TOOL_ENDPOINTS: Record<string, string> = {
run_bash: "run-bash",
read_file: "read-file",
write_file: "write-file",
edit_file: "edit-file",
grep: "grep",
};
async function callTool(name: string, args: Record<string, unknown>) {
const endpoint = TOOL_ENDPOINTS[name];
const res = await fetch(
`${ARG_BASE}/workspaces/${WORKSPACE_ID}/tools/${endpoint}`,
{
method: "POST",
headers: {
"X-API-Key": ARG_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(args),
}
);
return res.json();
}
// Agent loop
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: "user", content: "List the files in /home/user/project and read the README" },
];
while (true) {
const response = await openai.chat.completions.create({
model: "o4-mini",
tools,
messages,
});
const choice = response.choices[0];
messages.push(choice.message);
if (choice.finish_reason === "tool_calls") {
for (const call of choice.message.tool_calls!) {
const result = await callTool(call.function.name, JSON.parse(call.function.arguments));
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
});
}
} else {
console.log(choice.message.content);
break;
}
}
import json
import requests
import openai
WORKSPACE_ID = "ws_123"
ARG_API_KEY = "YOUR_API_KEY"
ARG_BASE = "https://api.arg.ai/api"
# Define arg.ai workspace tools
tools = [
{
"type": "function",
"function": {
"name": "run_bash",
"description": "Run a shell command in the workspace sandbox",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
},
},
},
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read a file from the workspace",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"offset": {"type": "integer"},
"limit": {"type": "integer"},
},
"required": ["path"],
},
},
},
{
"type": "function",
"function": {
"name": "write_file",
"description": "Write a file to the workspace",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"content": {"type": "string"},
},
"required": ["path", "content"],
},
},
},
{
"type": "function",
"function": {
"name": "edit_file",
"description": "Replace a string in a file",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"old_string": {"type": "string"},
"new_string": {"type": "string"},
},
"required": ["path", "old_string", "new_string"],
},
},
},
{
"type": "function",
"function": {
"name": "grep",
"description": "Search file contents with a regex pattern",
"parameters": {
"type": "object",
"properties": {
"pattern": {"type": "string"},
"path": {"type": "string"},
"include": {"type": "string"},
},
"required": ["pattern"],
},
},
},
]
TOOL_ENDPOINTS = {
"run_bash": "run-bash",
"read_file": "read-file",
"write_file": "write-file",
"edit_file": "edit-file",
"grep": "grep",
}
def call_tool(name: str, args: dict) -> dict:
endpoint = TOOL_ENDPOINTS[name]
resp = requests.post(
f"{ARG_BASE}/workspaces/{WORKSPACE_ID}/tools/{endpoint}",
headers={"X-API-Key": ARG_API_KEY, "Content-Type": "application/json"},
json=args,
)
return resp.json()
# Agent loop
client = openai.OpenAI()
messages = [
{"role": "user", "content": "List the files in /home/user/project and read the README"},
]
while True:
response = client.chat.completions.create(
model="o4-mini",
tools=tools,
messages=messages,
)
choice = response.choices[0]
messages.append(choice.message)
if choice.finish_reason == "tool_calls":
for call in choice.message.tool_calls:
result = call_tool(call.function.name, json.loads(call.function.arguments))
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
else:
print(choice.message.content)
break
How it works
- Define tools — each arg.ai tool endpoint is registered as a function tool with matching parameters
- Run the agent loop — send messages to OpenAI; when the model returns
tool_calls, execute them against the arg.ai API - Return results — pass tool output back as
toolmessages so the model can continue