Skip to main content

Copass agent primitives for the Anthropic Managed Agents API

Project description

copass-anthropic-agents

Anthropic Managed Agents backend for Copass. Depends on copass-core-agents, which owns the vendor-neutral primitives (BaseAgent, AgentTool, AgentBackend ABC, events, AgentScope, registries, etc.) shared across every provider-specific Copass SDK.

This package owns the Anthropic-specific piece — ManagedAgentBackend and the CopassManagedAgent convenience subclass — and re-exports the core primitives for one-line dev imports. Either of these works for consumers:

from copass_anthropic_agents import BaseAgent, CopassManagedAgent  # convenient
# or
from copass_core_agents import BaseAgent                           # explicit boundary
from copass_anthropic_agents import CopassManagedAgent

Pairs with Copass's server-side router — both sides import the same code so the agent runtime is a single codepath, whether the agent runs in the dev's process (deep-integration) or server-side via Copass's Router endpoint (thin client).

Install

pip install copass-anthropic-agents

Requires anthropic>=0.93.0 (first version shipping the beta.agents / beta.environments / beta.sessions surface).

Quickstart

import os
from copass_anthropic_agents import (
    AgentInvocationContext,
    AgentScope,
    CopassManagedAgent,
)

agent = CopassManagedAgent(
    identity="support",
    system_prompt="You are a support agent.",
    anthropic_api_key=os.environ["ANTHROPIC_API_KEY"],
)

ctx = AgentInvocationContext(scope=AgentScope(user_id="u-123"))
result = await agent.run(
    messages=[{"role": "user", "content": "Hello"}],
    context=ctx,
)
print(result.final_text)

Streaming

from copass_anthropic_agents import (
    AgentFinish,
    AgentTextDelta,
    AgentToolCall,
    AgentToolResult,
)

async for evt in agent.stream(messages, context=ctx):
    match evt:
        case AgentTextDelta(text=chunk):
            print(chunk, end="", flush=True)
        case AgentToolCall(name=name, arguments=args):
            print(f"\n[tool-call] {name}")
        case AgentToolResult(name=name, result=result):
            print(f"\n[tool-result] {name}")
        case AgentFinish(stop_reason=reason, session_id=sid):
            print(f"\n[finish] {reason} session={sid}")

Multi-turn continuation

The Anthropic managed-agent session holds conversation state. Capture AgentFinish.session_id and thread it back on the next turn:

from copass_anthropic_agents import SESSION_ID_HANDLE

ctx2 = AgentInvocationContext(
    scope=AgentScope(user_id="u-123"),
    handles={SESSION_ID_HANDLE: prior_session_id},
)

Custom tools

See AgentTool + AgentToolRegistry + AgentToolResolver. Tools are dispatched locally on tool-use events; provider handles the rest.

from copass_anthropic_agents import AgentTool, AgentToolRegistry, ToolSpec

class EchoTool(AgentTool):
    @property
    def spec(self):
        return ToolSpec(
            name="echo",
            description="Echo a message back.",
            input_schema={
                "type": "object",
                "properties": {"text": {"type": "string"}},
                "required": ["text"],
            },
        )

    async def invoke(self, arguments, *, context=None):
        return {"echoed": arguments["text"]}


tools = AgentToolRegistry()
tools.add(EchoTool())
agent = CopassManagedAgent(
    identity="support",
    system_prompt="You are helpful.",
    anthropic_api_key=os.environ["ANTHROPIC_API_KEY"],
    tools=tools,
)

Rate-limit note

ManagedAgentBackend caches Anthropic-side agent + environment resources per-instance by a fingerprint of (model, system_prompt, tool_schemas). Reusing the backend across requests shares the cache; constructing a fresh backend per request re-creates resources and hits Anthropic's 60-creates/min limit fast. Use the backend as a process-wide singleton in server contexts.

Context injection (deferred)

A future release will pull Copass's Context Window into system_prompt via the reserved {{copass_context}} placeholder (see spec/context-placeholders.md in this monorepo). Not wired in this release — injection would change system_prompt per invocation, invalidating the backend's fingerprint cache.

Conversation metadata: speaker + participants

When auto_record=True (the default when window is supplied), the agent constructs a CopassTurnRecorder that auto-populates the envelope's typed metadata on every turn:

  • speaker — set from the author constructor arg on assistant turns; falls back to "User" / "Assistant" from the role on user turns.
  • participants — defaults to ["User", author or "Assistant"]. The roster rides on every turn so downstream retrieval can resolve second-person pronouns.
agent = CopassManagedAgent(
    identity="support",
    system_prompt="You are helpful.",
    anthropic_api_key=os.environ["ANTHROPIC_API_KEY"],
    window=window,
    author="agent:support-bot",   # → speaker on every assistant turn
    # participants auto-defaults to ["User", "agent:support-bot"]
)

If you need a richer roster (multi-party threads) or a logged-in user identity, build a CopassTurnRecorder directly with participants=... and user_speaker="Alice", then pass auto_record=False to the agent and drive recording yourself. See copass-context-agents for the recorder surface and full envelope semantics.

License

MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

copass_anthropic_agents-1.4.8.tar.gz (92.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

copass_anthropic_agents-1.4.8-py3-none-any.whl (55.2 kB view details)

Uploaded Python 3

File details

Details for the file copass_anthropic_agents-1.4.8.tar.gz.

File metadata

  • Download URL: copass_anthropic_agents-1.4.8.tar.gz
  • Upload date:
  • Size: 92.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for copass_anthropic_agents-1.4.8.tar.gz
Algorithm Hash digest
SHA256 5369f628a29d9d24f4bae9fb95c2f8a7ddbe5528ea8c04f41b7c0f8be7093032
MD5 830ff8cf166152ba78cbba4b853b0f7d
BLAKE2b-256 5f38284897cc62b30c5ea2f526ec34dd7e4652c0078cae8abdec0e8888dc1e24

See more details on using hashes here.

Provenance

The following attestation bundles were made for copass_anthropic_agents-1.4.8.tar.gz:

Publisher: release-python.yml on olane-labs/copass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file copass_anthropic_agents-1.4.8-py3-none-any.whl.

File metadata

File hashes

Hashes for copass_anthropic_agents-1.4.8-py3-none-any.whl
Algorithm Hash digest
SHA256 bba5d99df7f298921ce9a36f7cbd07828ee91d54708cb2834ec8b8fa43d8ec99
MD5 12cbd7bf6c610b52f6dd82b3cb70df5e
BLAKE2b-256 3f34009f66b5e035059786484ba40935ef6c85474e2a78b3545df1c05d17341a

See more details on using hashes here.

Provenance

The following attestation bundles were made for copass_anthropic_agents-1.4.8-py3-none-any.whl:

Publisher: release-python.yml on olane-labs/copass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page