PDF Input Local Agent

Code

1from pathlib import Path
2
3from kern.agent import Agent
4from kern.media import File
5from kern.models.anthropic import Claude
6from kern.utils.media import download_file
7
8pdf_path = Path(__file__).parent.joinpath("ThaiRecipes.pdf")
9
10# Download the file using the download_file function
11download_file(
12 "https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf", str(pdf_path)
13)
14
15agent = Agent(
16 model=Claude(id="claude-sonnet-4-20250514"),
17 markdown=True,
18)
19
20agent.print_response(
21 "Summarize the contents of the attached file.",
22 files=[
23 File(
24 filepath=pdf_path,
25 ),
26 ],
27)
28run_response = agent.get_last_run_output()
29print("Citations:")
30print(run_response.citations)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export ANTHROPIC_API_KEY=xxx

Install dependencies

1uv pip install -U anthropic kern-ai

Run Agent

1python cookbook/11_models/anthropic/pdf_input_local.py