> For the complete documentation index, see [llms.txt](https://docs.scanner.dev/scanner/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.scanner.dev/scanner/using-scanner-complete-feature-reference/querying-and-analysis/aggregation-functions/tail.md).

# tail()

`tail([n])` returns the last `n` rows from the input stream. `n` must be between 1 and 10,000 and defaults to 10.

When applied directly to a query filter, rows are ordered by timestamp (`@scnr.datetime`), and the last (newest) rows are returned.

When applied after another aggregation (for example, `groupbycount ... | tail 3`), the last rows in the order produced by that aggregation are returned.

If your query matches fewer than `n` rows, all matching rows are returned. The result may also be smaller than `n` rows if the total result size exceeds 128MB (or `max_bytes` if using the [API](/scanner/using-scanner-complete-feature-reference/developer-tools/api/ad-hoc-queries.md)). In either case, the returned rows are always the true last rows — no later rows are omitted.

See also: [head()](/scanner/using-scanner-complete-feature-reference/querying-and-analysis/aggregation-functions/head.md), which returns the first rows.

## Returns

A table with up to `n` rows, preserving all original columns.

## Examples

Get the 10 most recent GuardDuty findings (default `n`).

```python
@scnr.source_type: "aws:guardduty"
| tail
```

Get the last 50 login events and count them by user.

```python
eventName: "ConsoleLogin"
| tail 50
| groupbycount userIdentity.arn
```

Get the 3 least frequent users by login count.

```python
eventName: "ConsoleLogin"
| groupbycount userIdentity.arn
| tail 3
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.scanner.dev/scanner/using-scanner-complete-feature-reference/querying-and-analysis/aggregation-functions/tail.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
