This module provides a Python client for interacting with the Codegen AI agents API.
The Codegen Agent SDK is included as part of the Codegen package. Ensure you have the latest version installed:
pip install codegenfrom codegen.agents.agent import Agent
# Initialize the Agent with your organization ID and API token
agent = Agent(
org_id="11", # Your organization ID
token="your_api_token_here", # Your API authentication token
base_url="https://codegen-sh-rest-api.modal.run", # Optional - defaults to this URL
)
# Run an agent with a prompt
task = agent.run(prompt="Which github repos can you currently access?")
# Check the initial status
print(task.status) # Returns the current status of the task (e.g., "queued", "in_progress", etc.)
# Refresh the task to get updated status
task.refresh()
# Check the updated status
print(task.status)
# Once task is complete, you can access the result
if task.status == "completed":
print(task.result)The Agent class is the main entry point for interacting with Codegen AI agents:
Agent(token: str, org_id: Optional[int] = None, base_url: Optional[str] = CODEGEN_BASE_API_URL)Parameters:
token(required): Your API authentication tokenorg_id(optional): Your organization ID. If not provided, defaults to environment variableCODEGEN_ORG_IDor "1"base_url(optional): API base URL. Defaults to "https://codegen-sh-rest-api.modal.run"
run(prompt: str) -> AgentTaskRuns an agent with the given prompt.
Parameters:
prompt(required): The instruction for the agent to execute
Returns:
- An
AgentTaskobject representing the running task
get_status() -> Optional[Dict[str, Any]]Gets the status of the current task.
Returns:
- A dictionary containing task status information (
id,status,result), orNoneif no task has been run
The AgentTask class represents a running or completed agent task:
id: The unique identifier for the taskorg_id: The organization IDstatus: Current status of the task (e.g., "queued", "in_progress", "completed", "failed")result: The task result (available when status is "completed")
refresh() -> NoneRefreshes the task status from the API.
CODEGEN_ORG_ID: Default organization ID (used iforg_idis not provided)
Handle potential API errors using standard try/except blocks:
try:
task = agent.run(prompt="Your prompt here")
task.refresh()
print(task.status)
except Exception as e:
print(f"Error: {e}")