"""S3 export target — upload records to your own S3 bucket."""

import anthropic

import caliper

######################################################################
# Initialize with S3 target
######################################################################


def init_caliper() -> None:
    # Reads credentials from environment variables — see .env.example for the full list.
    # Load them before running: set -a && source .env && set +a
    # batch_size=1 writes one record per S3 file instead of batching
    caliper.init(target="s3", batch_size=1, s3_partition="none")

    # For S3-compatible stores (MinIO, R2, etc.), add s3_endpoint:
    #   caliper.init(target="s3", s3_endpoint="http://localhost:9000", s3_partition="none")


######################################################################
# Make some instrumented calls
######################################################################


def make_calls(client: anthropic.Anthropic) -> None:
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=128,
        messages=[{"role": "user", "content": "Say hello in exactly 5 words."}],
    )
    print(response.content[0].text)

    with client.messages.stream(
        model="claude-sonnet-4-20250514",
        max_tokens=128,
        messages=[{"role": "user", "content": "Tell me a one-sentence joke."}],
    ) as stream:
        for chunk in stream.text_stream:
            print(chunk, end="", flush=True)
        print()


######################################################################
# Main
######################################################################


def main() -> None:
    init_caliper()
    client = anthropic.Anthropic()
    make_calls(client)

    # Flush remaining records to S3 before exit
    caliper.shutdown()

    # Records land in S3 at keys like:
    #   records/batch_<ulid>.json
    #   annotations/batch_<ulid>.json
    print("\nRecords exported to S3. Check your bucket for new batch files.")


if __name__ == "__main__":
    main()
