Skip to main content

metorial-anthropic

Anthropic (Claude) provider integration for Metorial.

Installation

pip install metorial anthropic

Quick Start

import asyncio
from metorial import Metorial, MetorialAnthropic
from anthropic import AsyncAnthropic

metorial = Metorial(api_key="your-metorial-api-key")
anthropic = AsyncAnthropic(api_key="your-anthropic-api-key")

async def main():
    async def session_handler(session):
        messages = [{"role": "user", "content": "What's the latest news?"}]

        for _ in range(10):
            response = await anthropic.messages.create(
                model="claude-sonnet-4-5",
                max_tokens=1024,
                messages=messages,
                tools=session["tools"]
            )

            tool_calls = [b for b in response.content if b.type == "tool_use"]
            if not tool_calls:
                print(response.content[0].text)
                break

            tool_responses = await session["callTools"](tool_calls)
            messages.append({"role": "assistant", "content": response.content})
            messages.append(tool_responses)

        await session["closeSession"]()

    await metorial.with_provider_session(
        MetorialAnthropic,
        {"serverDeployments": [{"serverDeploymentId": "your-server-deployment-id"}]},
        session_handler
    )

asyncio.run(main())

Streaming

import asyncio
from metorial import Metorial, MetorialAnthropic
from anthropic import AsyncAnthropic

metorial = Metorial(api_key="your-metorial-api-key")
anthropic = AsyncAnthropic(api_key="your-anthropic-api-key")

async def main():
    async def session_handler(session):
        messages = [{"role": "user", "content": "What's the latest news?"}]

        for _ in range(10):
            async with anthropic.messages.stream(
                model="claude-sonnet-4-5",
                max_tokens=1024,
                messages=messages,
                tools=session["tools"]
            ) as stream:
                async for text in stream.text_stream:
                    print(text, end="", flush=True)
                response = await stream.get_final_message()

            tool_calls = [b for b in response.content if b.type == "tool_use"]
            if not tool_calls:
                print()
                break

            tool_responses = await session["callTools"](tool_calls)
            messages.append({"role": "assistant", "content": response.content})
            messages.append(tool_responses)

        await session["closeSession"]()

    await metorial.with_provider_session(
        MetorialAnthropic,
        {
            "serverDeployments": [{"serverDeploymentId": "your-server-deployment-id"}],
            "streaming": True,  # Required for streaming with tool calls
        },
        session_handler
    )

asyncio.run(main())

Supported Models

All Anthropic Claude models that support tool calling:

  • claude-sonnet-4-5: Latest Claude Sonnet
  • claude-opus-4: Most capable Claude model
  • claude-3-5-sonnet-20241022: Claude 3.5 Sonnet
  • claude-3-5-haiku-20241022: Fastest Claude 3.5 model

Session Object

async def session_handler(session):
    tools = session["tools"]           # Tool definitions in Anthropic format
    call_tools = session["callTools"]  # Execute tools and get responses
    close_session = session["closeSession"]  # Close the session when done

Error Handling

from metorial import MetorialAPIError

try:
    await metorial.with_provider_session(...)
except MetorialAPIError as e:
    print(f"API Error: {e.message} (Status: {e.status})")
except Exception as e:
    print(f"Unexpected error: {e}")

License

MIT License - see LICENSE file for details.

Download files

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

Source Distribution

metorial_anthropic-1.0.5.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

metorial_anthropic-1.0.5-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file metorial_anthropic-1.0.5.tar.gz.

File metadata

  • Download URL: metorial_anthropic-1.0.5.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for metorial_anthropic-1.0.5.tar.gz
Algorithm Hash digest
SHA256 9023ebed6ca5925ace95964fa694690bf7e6d8548463a1ead6f5d0bcb11d36d4
MD5 9e473e097a4b485965c2780f9b56823a
BLAKE2b-256 ebee73f933e1c5ebe1ddfea5316d4d9983c17a033d08925d939c8bbf52110ae9

See more details on using hashes here.

Provenance

The following attestation bundles were made for metorial_anthropic-1.0.5.tar.gz:

Publisher: release.yml on metorial/metorial-python

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

File details

Details for the file metorial_anthropic-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for metorial_anthropic-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 bcefbd560da3b2eba13a2c9837f489ef71eb22dc52625cf98dca3b1fcb775e6c
MD5 52fbdc846ff5763f3dc4a77cc46aecda
BLAKE2b-256 5af000a8f069a30277da8d0a0d9c583122eb91e66585488f6790b8e0b03bce36

See more details on using hashes here.

Provenance

The following attestation bundles were made for metorial_anthropic-1.0.5-py3-none-any.whl:

Publisher: release.yml on metorial/metorial-python

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

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

Supported by

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